Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notice: Undefined offset: 2 on src/App/MyBundle/Model/om/BaseComponentProductMapsQuery.php at line 241 #342

Open
liamsorsby opened this issue Jul 2, 2015 · 1 comment
Labels

Comments

@liamsorsby
Copy link

I can change the options that component without a problem using the generated check boxes however, if I decide that I want to untick all of the checkboxes then I get the following error:

 Notice: Undefined offset: 2
500 Internal Server Error - ContextErrorException 
in src/App/MyBundle/Model/om/BaseComponentProductMapsQuery.php at line 241 

Stack Trace:

           $cton0 = $this->getNewCriterion(ComponentProductMapsPeer::ID, $key[0], Criteria::EQUAL);
            $cton1 = $this->getNewCriterion(ComponentProductMapsPeer::COMPONENT_ID, $key[1], Criteria::EQUAL);
            $cton0->addAnd($cton1);
            $cton2 = $this->getNewCriterion(ComponentProductMapsPeer::PRODUCT_ID, $key[2], Criteria::EQUAL);
            $cton0->addAnd($cton2);
            $this->addOr($cton0);
        }

Schema:

<?xml version="1.0" encoding="UTF-8"?>

<database name="default" namespace="App\MyBundle\Model" defaultIdMethod="native">

    <table name="component">
        <column name="id"
                type="integer"
                required="true"
                autoIncrement="true"
                primaryKey="true"/>
        <column name="component_type"
                type="integer"/>
        <column name="component_name"
                type="varchar"/>
        <column name="weight"
                type="decimal"
                size="10"
                scale="2" />
        <column name="height"
                type="decimal"
                size="10"
                scale="2" />
        <column name="length"
                type="decimal"
                size="10"
                scale="2" />
        <column name="depth"
                type="decimal"
                size="10"
                scale="2" />
        <column name="single_price"
                type="boolean"/>
        <column name="global_component"
                type="boolean"
                required="true"
                default="0"/>
        <foreign-key foreignTable="form_type">
            <reference local="component_type" foreign="id" />
        </foreign-key>
        <vendor type="mysql">
            <parameter name="Engine" value="InnoDB" />
            <parameter name="Charset" value="utf8" />
        </vendor>
    </table>

    <table name="component_product_maps" isCrossRef="true">
        <column name="id"
                type="integer"
                required="true"
                autoIncrement="true"
                primaryKey="true"/>
        <column name="component_id"
                type="integer"
                primaryKey="true"/>
        <column name="product_id"
                type="integer"
                primaryKey="true"/>
        <foreign-key foreignTable="product" onDelete="cascade">
            <reference local="product_id" foreign="id" />
        </foreign-key>
        <foreign-key foreignTable="component" onDelete="cascade">
            <reference local="component_id" foreign="id" />
        </foreign-key>
        <vendor type="mysql">
            <parameter name="Engine" value="InnoDB" />
            <parameter name="Charset" value="utf8" />
        </vendor>
    </table>

    <table name="product">
        <column name="id"
                type="integer"
                required="true"
                autoIncrement="true"
                primaryKey="true"/>
        <column name="image"
                type="varchar"
                size="150"/>
        <column name="sku"
                type="BIGINT"/>
        <column name="product_name"
                type="varchar"
                size="150"
                required="true"/>
        <column name="stock_code"
                type="varchar"
                size="150"
                required="true"/>
        <column name="long_description"
                type="longvarchar"
                required="true"/>
        <column name="short_description"
                type="varchar"
                size="250"
                required="true"/>
        <column name="weight"
                type="decimal"
                size="10"
                scale="2"
                required="true"/>
        <column name="length"
                type="decimal"
                size="10"
                scale="2"
                required="true"/>
        <column name="height"
                size="10"
                scale="2"
                type="decimal"
                required="true"/>
        <column name="depth"
                type="decimal"
                size="10"
                scale="2"
                required="true"/>
        <column name="url"
                type="varchar"
                size="250"
                required="true"/>
        <column name="meta_description"
                type="varchar"
                size="250"
                required="true"/>
        <column name="meta_keywords"
                type="varchar"
                size="700"
                required="true"/>
        <column name="meta_title"
                type="varchar"
                size="255"
                required="true"/>
        <column name="old_url"
                type="varchar"
                size="250"/>
        <unique>
            <unique-column name="url"/>   
        </unique>
        <unique>
            <unique-column name="meta_title"/>
        </unique>
        <unique>
            <unique-column name="sku"/> 
        </unique>
        <vendor type="mysql">
            <parameter name="Engine" value="InnoDB" />
            <parameter name="Charset" value="utf8" />
        </vendor>
    </table>
</database>

FormBuilder section of the Form:

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('image', "file", array(
            "data_class" => null
        ));
        $builder->add('sku');
        $builder->add('productName', null, array(
            "label" => false
        ));
        $builder->add('stockCode');
        $builder->add('longDescription', 'textarea', array(
            "label" => false
        ));
        $builder->add('shortDescription');
        $builder->add('weight');
        $builder->add('length');
        $builder->add('height');
        $builder->add('depth');
        $builder->add('url');
        $builder->add('metaDescription');
        $builder->add('metaKeywords');
        $builder->add('metaTitle');
        $builder->add('oldUrl');
        $builder->add('price', "money", array(
            "mapped" =>false,
            'divisor' => 100,
            'currency' => "USD"
            ));
        $builder->add('components', 'model', array(
            'class' => 'App\MyBundle\Model\Component',
            'query' => ComponentQuery::create()->filterByGlobalComponent(1)->orderByComponentName(),
            'property' => 'component_name',
            'label' => "Global Components",
            'required' => false,
            'multiple' => true,
            'expanded' => true,
            'by_reference' => false
        ));
    }
@liamsorsby
Copy link
Author

The issue seems to be on the BaseComponentProductMapsQuery function that has been prebuilt.

This is Where the error occurs:

/**
     * Filter the query by a list of primary keys
     *
     * @param     array $keys The list of primary key to use for the query
     *
     * @return ComponentProductMapsQuery The current query, for fluid interface
     */
    public function filterByPrimaryKeys($keys)
    {
        if (empty($keys)) {
            return $this->add(null, '1<>1', Criteria::CUSTOM);
        }
        foreach ($keys as $key) {
            $cton0 = $this->getNewCriterion(ComponentProductMapsPeer::ID, $key[0], Criteria::EQUAL);
            $cton1 = $this->getNewCriterion(ComponentProductMapsPeer::COMPONENT_ID, $key[1], Criteria::EQUAL);
            $cton0->addAnd($cton1);
            $cton2 = $this->getNewCriterion(ComponentProductMapsPeer::PRODUCT_ID, $key[2], Criteria::EQUAL);
            $cton0->addAnd($cton2);
            $this->addOr($cton0);
        }

        return $this;
    }

As you can see from the BaseProduct function below (part of it):

protected function doSave(PropelPDO $con)
    {
        $affectedRows = 0; // initialize var to track total num of affected rows
        if (!$this->alreadyInSave) {
            $this->alreadyInSave = true;

            if ($this->isNew() || $this->isModified()) {
                // persist changes
                if ($this->isNew()) {
                    $this->doInsert($con);
                } else {
                    $this->doUpdate($con);
                }
                $affectedRows += 1;
                $this->resetModified();
            }

            if ($this->componentsScheduledForDeletion !== null) {
                if (!$this->componentsScheduledForDeletion->isEmpty()) {
                    $pks = array();
                    $pk = $this->getPrimaryKey();
                    foreach ($this->componentsScheduledForDeletion->getPrimaryKeys(false) as $remotePk) {
                        $pks[] = array($pk, $remotePk);
                    }
                    ComponentProductMapsQuery::create()
                        ->filterByPrimaryKeys($pks)
                        ->delete($con);
                    $this->componentsScheduledForDeletion = null;
                }

                foreach ($this->getComponents() as $component) {
                    if ($component->isModified()) {
                        $component->save($con);
                    }
                }
            } elseif ($this->collComponents) {
                foreach ($this->collComponents as $component) {
                    if ($component->isModified()) {
                        $component->save($con);
                    }
                }
            }

From the line if ($this->componentsScheduledForDeletion !== null) { you can see that the $pks[] array only contain key 0 and 1 but the function filterByPrimaryKeys requires three parameters. The first one that has been passed through so $pks[0] should be ComponentProductMapsPeer::ID however ComponentProductMapsPeer::PRODUCT_ID is passed through.

I fixed it by wrapping the BaseComponentProductMapsQuery in an if statement

 if (count($key) == 2){
                $cton1 = $this->getNewCriterion(ComponentProductMapsPeer::COMPONENT_ID, $key[1], Criteria::EQUAL);
                $cton2 = $this->getNewCriterion(ComponentProductMapsPeer::PRODUCT_ID, $key[0], Criteria::EQUAL);
                $cton1->addAnd($cton2);
                $this->addOr($cton1);
            }else{
               // original code
            }

As I'm not sure if this original code is actually needed or not and it's only a temporary fix as when I rebuild the model it will work. There more than likely will be a more elegant solution to this.

@havvg havvg added the Bug label Jan 22, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants