please help me ManyToOne JoinColumn(name="model_id") is NULL

New topics about Symfony 2 should go here

Moderators: dcobalt, tiagojsag

please help me ManyToOne JoinColumn(name="model_id") is NULL

Postby zhuzuoling » Sat Apr 14, 2012 3:39 am

model entity
Code: Select all
<?php

namespace Backend\GoodsBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Doctrine\Common\Collections\ArrayCollection;
use Backend\GoodsBundle\Entity\Attribute;

/**
 * Backend\GoodsBundle\Entity\Model
 *
 * @ORM\Table()
 * @ORM\Entity
 * @UniqueEntity(fields="name", message="模型名称已经存在")
 */
class Model
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string $name
     * @Assert\NotBlank(message="品牌名称不能为空")
     * @ORM\Column(name="name", type="string", length=50)
     */
    private $name;

    /**
     * @var text $spec_ids
     *
     * @ORM\Column(name="spec_ids", type="text")
     */
    private $spec_ids;
   
    /**
     * @ORM\OneToMany(targetEntity="Attribute", mappedBy="model",cascade={"persist", "remove", "merge"}, orphanRemoval=true)
     */
    private $attributes;
   
    /**
     * @var text $specs
     *
     */
    public $specs;
   
    public function __construct()
    {
        $this->attributes = new ArrayCollection();
    }
    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set spec_ids
     *
     * @param text $specIds
     */
    public function setSpecIds($specIds)
    {
        foreach ($specIds as $key => $val){
            $array[$key]['id'] = $val->getId();
        };

        $this->spec_ids = serialize($array);
    }

    /**
     * Get spec_ids
     *
     * @return text
     */
    public function getSpecIds()
    {
        if(empty($this->spec_ids))
            return $this->spec_ids;
        else
            return unserialize($this->spec_ids);
    }
   
    /**
     * Set spec_ids
     *
     * @param text $specs
     */
    public function setSpecs($specs)
    {
            foreach ($specs as $key => $val){
                $array[$key]['id'] = $val->getId();
            };
            $this->spec_ids = serialize($array);
       
    }

    /**
     * Get spec_ids
     *
     * @return text
     */
    public function getSpecs()
    {
   
        return $this->specs;
    }

    /**
     * Set attributes
     *
     * @param text $attributes
     */
    public function setAttributes($attributes)
    {
        $this->attributes = $attributes;
    }

    /**
     * Get attributes
     *
     * @return text
     */
    public function getAttributes()
    {
        return $this->attributes;
    }
   
    public function __toString()
    {
        return $this->name;
    }
}

Attribute enity
Code: Select all
<?php
namespace Backend\GoodsBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * Backend\GoodsBundle\Entity\Attribute
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Attribute
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
   
    /**
     * @var smallint $name
     *
     * @ORM\Column(name="name",type="string", length=50)
     */
    private $name;

    /**
     * @var smallint $input_type
     *
     * @ORM\Column(name="input_type", type="smallint")
     */
    private $input_type;

    /**
     * @var text $value
     *
     * @ORM\Column(name="value", type="text")
     */
    private $value;

    /**
     * @var smallint $search
     *
     * @ORM\Column(name="search", type="smallint")
     */
    private $search;
   
    /**
     * @var smallint $type
     *
     */
    private $type;
   
       
    /**
     * @var smallint $searchs
     *
     */
    private $searchs;

    /**
     * @ORM\ManyToOne(targetEntity="Model", inversedBy="attributes")
     * @ORM\JoinColumn(name="model_id", referencedColumnName="id", nullable =false)
     */
    private $model;
   

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }
   
    /**
     * Set name
     *
     * @param string $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }   

    /**
     * Set input_type
     *
     * @param smallint $inputType
     */
    public function setInputType($inputType)
    {
        $this->input_type = $inputType;
    }

    /**
     * Get input_type
     *
     * @return smallint
     */
    public function getInputType()
    {
        return $this->input_type;
    }

    /**
     * Set value
     *
     * @param text $value
     */
    public function setValue($value)
    {
        $this->value = $value;
    }

    /**
     * Get value
     *
     * @return text
     */
    public function getValue()
    {
        return $this->value;
    }

    /**
     * Set search
     *
     * @param smallint $search
     */
    public function setSearch($search)
    {
        $this->search = $search;
    }

    /**
     * Get search
     *
     * @return smallint
     */
    public function getSearch()
    {
        return $this->search;
    }

    /**
     * Set model_id
     *
     * @param smallint $model_id
     */
    public function setModelId($modelId)
    {
        $this->model_id = $modelId;
    }

    /**
     * Get model
     *
     * @return smallint
     */
    public function getModelId()
    {
        return $this->model_id;
    }   
   
    /**
     * Set model
     *
     * @param smallint $model
     */
    public function setModel($model)
    {
        $this->model = $model;
    }

    /**
     * Get model
     *
     * @return smallint
     */
    public function getModel()
    {
        return $this->model;
    }   
   
    /**
     * Set type
     *
     * @param smallint $type
     */
    public function setType($type)
    {
        $this->input_type = $type;
    }

    /**
     * Get input_type
     *
     * @return smallint
     */
    public function getType()
    {
        return $this->type;
    }
   
    /**
     * Set searchs
     *
     * @param smallint $searchs
     */
    public function setSearchs($searchs)
    {
        $this->search = $searchs;
    }

    /**
     * Get searchs
     *
     * @return smallint
     */
    public function getSearchs()
    {
        return $this->searchs;
    }
    public function __toString()
    {
        return $this->name;
    }
}


Action
Code: Select all
   
    /**
     * @Route("/model/add/{id}", name="backend_model_add", defaults={"id"="0"})
     * @Template()
     */
    public function addAction($id)
    {
         if($id){
            $model = $this->getDoctrine()->getRepository('BackendGoodsBundle:Model')->find($id);
            if (!$model) {
                throw $this->createNotFoundException('模型不存在');
            }
        }else{
            $model = new Model();
        }
        $form = $this->createForm(new ModelType, $model);
        $request = $this->getRequest();
        if($request->getMethod() == 'POST'){
            $form->bindRequest($request);
            if ($form->isValid()) {
                $em = $this->getDoctrine()->getEntityManager();
                $em->persist($model);
               
//                foreach ($model->getAttributes() as $key=>$attr){
//                    $em->persist($attr);
//                }
                $em->flush();
                //$model->setAttr($em);
                         
            }
        }
        return array('form'=>$form->createView(), 'model'=>$model);
    }


Type
ModelType
Code: Select all
<?php
namespace Backend\GoodsBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Backend\GoodsBundle\Form\AttributeType;

class ModelType extends AbstractType{

   public function buildForm(FormBuilder $builder, array $option)
   {
      $builder->add('name', 'text', array('label'=>'模型名称'))
                ->add('specs', 'entity', array(
                    'class' => 'BackendGoodsBundle:Spec',
                    'label'=>'规格','multiple'=>true
                    ))
                ->add('attributes', 'collection', array(
                    // each item in the array will be an "email" field
                    'type' => new AttributeType(),
                    'allow_add' => true,
                    'allow_delete'=>true,
                    'by_reference' => false,
                ));
   }
        public function getDefaultOptions(array $options) {
           
            return array( 'data_class' => 'Backend\GoodsBundle\Entity\Model' );
           
        }
   public function getName()
   {
      return 'backend_goodsbundle_modeltype';
   }
}

Attribute Type
Code: Select all
<?php
namespace Backend\GoodsBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class AttributeType extends AbstractType{

   public function buildForm(FormBuilder $builder, array $option)
   {
      $builder
                ->add('name', 'text', array('label'=>'属性名称'))
                ->add('type', 'choice', array(
                    'choices' => array(
                        '1' => '单选框',
                        '2' => '复选框',
                        '3' => '下拉框',
                        '4' => '输入框',
                        ),'label'=>'属性显示方式'))
                ->add('value', 'text', array('label'=>'属性值(选项之间用逗号\',\'做分割)'))
                ->add('searchs', 'checkbox', array('label'=>'是否支持搜索','required'  => false));
               
   }
       
        public function getDefaultOptions(array $options) {
           
            return array( 'data_class' => 'Backend\GoodsBundle\Entity\Attribute' );
           
        }
   public function getName()
   {
      return 'backend_goodsbundle_attributetype';
   }
}



ERROR SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'model_id' cannot be null
zhuzuoling
Junior Member
 
Posts: 1
Joined: Wed Feb 08, 2012 12:49 pm

Return to General Symfony 2 discussion

Who is online

Users browsing this forum: Google Feedfetcher and 5 guests