php – Symfony2 Propel Collection undefined offset:2

前端之家收集整理的这篇文章主要介绍了php – Symfony2 Propel Collection undefined offset:2前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我们使用propel和Symfony2形式创建了一个集合.我们可以毫无问题地保存表单,我们可以使用该集合添加第二个选项.如果我们然后保存然后尝试添加第3个集合,我们会收到以下错误
Notice: Undefined offset: 2

堆栈跟踪

in src/app/MyBundle/Model/om/BaseLabelsLabelsLinesMapsQuery.PHP at line 241  

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

我已经在下面发布了相关代码,但是由于存在大量代码.我们想知道是否有人遇到过同样的问题.

我已经使用不同的代码发送了一个错误报告,这会产生相同的错误但是我没有得到回复.错误报告是here.

这是相关架构的片段:









<table name="labels_labels_lines_maps" isCrossRef="true">
    <column name="id"
            type="integer"
            required="true"
            autoIncrement="true"
            primaryKey="true"/>
    <column name="label_id"
            type="integer"
            primaryKey="true"/>
    <column name="label_lines_id"
            type="integer"
            primaryKey="true"/>
    <foreign-key foreignTable="labels" onDelete="cascade">
        <reference local="label_id" foreign="id"/>
    </foreign-key>
    <foreign-key foreignTable="labels_lines" onDelete="cascade">
        <reference local="label_lines_id" foreign="id"/>
    </foreign-key>
    <vendor type="MysqL">
        <parameter name="Engine" value="InnoDB" />
        <parameter name="Charset" value="utf8" />
    </vendor>
</table>

<table name="labels_lines">
    <column name="id"
            type="integer"
            required="true"
            autoIncrement="true"
            primaryKey="true"/>
    <column name="placeholder_text"
            type="varchar"
            size="150"/>
    <column name="font_id"
            type="integer"/>
    <column name="font_size"
            type="integer"/> 
    <column name="x_axis"
            type="integer"/>  
    <column name="y_axis"
            type="integer"/>
    <column name="width"
            type="integer"/>      
    <column name="height"
            type="integer"/>       
    <column name="colour"
            type="varchar"
            size="20"/>        
    <column name="angle"
            type="integer"/> 
    <column name="is_volume"
            type="boolean"/>
    <column name="is_percentage"
            type="boolean"/>
    <column name="is_productof"
            type="boolean"/>
    <column name="is_type"
            type="boolean"/>
    <column name="is_occasion"
            type="boolean"/>        
    <foreign-key foreignTable="font" onDelete="cascade">
        <reference local="font_id" foreign="id"/>
    </foreign-key>
    <vendor type="MysqL">
        <parameter name="Engine" value="InnoDB" />
        <parameter name="Charset" value="utf8" />
    </vendor>
</table>

<table name="occasion">
    <column name="id"
            type="integer"
            required="true"
            autoIncrement="true"
            primaryKey="true"/>
    <column name="occasion"
            type="varchar"
            size="200"/>

    <vendor type="MysqL">
        <parameter name="Engine" value="InnoDB" />
        <parameter name="Charset" value="utf8" />
    </vendor>
</table>

<table name="font">
    <column name="id"
            type="integer"
            required="true"
            autoIncrement="true"
            primaryKey="true"/>
    <column name="name"
            type="varchar"
            size="100"/>
    <column name="location"
            size="300"/>
    <vendor type="MysqL">
        <parameter name="Engine" value="InnoDB" />
        <parameter name="Charset" value="utf8" />
    </vendor>
</table>

下面是视图(没有任何样式):

{{ form_start(form) }}
{{ form_row(form._token) }}
<ul class="labelsliness list-group" data-prototype="{{ form_widget(form.labelsliness.vars.prototype)|e }}">
                    {% for lines in form.labelsliness %}
                        <li>{{ form(lines) }}</li>
                    {% endfor %}
                </ul>

                {{ form_row(form.save) }}

{{ form_end(form) }}

<script>
                    var $collectionHolder;

                    var $addLinesLink = $('<button class="add_line_link btn btn-primary">Add a line</button>');
                    var $newLinkLi = $('<li></li>').append($addLinesLink);

                    $(document).ready(function(){
                       $collectionHolder = $('ul.labelsliness');

                       $collectionHolder.append($newLinkLi);

                       $collectionHolder.data('index',$collectionHolder.find(':input').length);

                       $addLinesLink.on('click',function(e) {
                          e.preventDefault();

                          addLineForm($collectionHolder,$newLinkLi);
                       });
                    });

                    function addLineForm($collectionHolder,$newLinkLi) {
                        var prototype = $collectionHolder.data('prototype');

                        var index = $collectionHolder.data('index');

                        var newForm = prototype.replace('/__name__/g',index);

                        $collectionHolder.data('index',index + 1);

                        var $newFormLi = $('<li></li>').append(newForm);

                        $newFormLi.append('<button class="remove-line btn btn-danger">Remove</button>');

                        $newLinkLi.before($newFormLi);

                        $('.remove-line').click(function(e){
                           e.preventDefault();

                           $(this).parent().remove();

                           return false;
                        });
                    }
                </script>

表格处理:

public function buildForm(FormBuilderInterface $builder,array $options)
    {
       $builder
               ->add("labelsliness","collection",array(
                   "type" => new LabelsLinesType(),"allow_add" => true,"allow_delete" => true,"by_reference" => false
               ))
               ->add("save","submit");
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AJSharp\EPCBundle\Model\Labels',));
    }

    public function getName()
    {
        return "label_form";
    }

最后,下面是控制器.

public function editAction(Request $request,$id = null)
    {

        $labels = LabelsQuery::create()->findPk($id);

        $form = $this->createForm(new EditLabelType(),$labels);

        $form->handleRequest($request);

        if ($form->isValid()) {

            $labels->save();
            return $this->redirect($this->generateUrl("_admin_labels"));
        }

        return $this->render("AppLabelBundle:Admin:edit.html.twig",array("form" => $form->createView()));
    }
您的架构对我来说有点混乱.下面的这一位包括三个主键,其中两个作为外键,其中一个作为行的唯一标识符:
<table name="labels_labels_lines_maps" isCrossRef="true">
    <column name="id"
            type="integer"
            required="true"
            autoIncrement="true"
            primaryKey="true"/>
    <column name="label_id"
            type="integer"
            primaryKey="true"/>
    <column name="label_lines_id"
            type="integer"
            primaryKey="true"/>
    <foreign-key foreignTable="labels" onDelete="cascade">
        <reference local="label_id" foreign="id"/>
    </foreign-key>
    <foreign-key foreignTable="labels_lines" onDelete="cascade">
        <reference local="label_lines_id" foreign="id"/>
    </foreign-key>
    <vendor type="MysqL">
        <parameter name="Engine" value="InnoDB" />
        <parameter name="Charset" value="utf8" />
    </vendor>
</table>

我怀疑如果你选择一件事并坚持下去,你的很多麻烦都可能消失.删除id并让主复合键表示两个完全有效的外表,或者将PRIMARY KEY更改为每个外键上的UNIQUE约束,将主键保留为id.这也完全有效.最终,您的决定将基于您的设计要求.

另一个注意事项:您可能想要也可能不想做heavyIndexing,特别是如果您引用主键中的各个列.复合主键为所有三列创建索引,而不是单独创建每个列.这在您的项目中可能重要,也可能不重要,但我认为值得指出.

猜你在找的PHP相关文章