在关联(数组)上找到类型为Doctrine \ Common \ Collections \ ArrayCollection的实体,但期待(其他)

时间:2012-11-26 08:52:07

标签: php symfony doctrine

我得到的错误是:

  

在关联模型\ StoreBundle \ Entity \ campaign#campaignOperatingSystems上找到Doctrine \ Common \ Collections \ ArrayCollection类型的实体,但期望model \ StoreBundle \ Entity \ campaignOS

这是campaignOS总结:

/**
 * model\StoreBundle\Entity\campaignOS
 * @ORM\Table(name="campaign_os")
 * @ORM\Entity
 */
class campaignOS extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('OS', 'entity', array(
                'class' => 'modelStoreBundle:listOS',
                'property' => 'OsName',
                'multiple' => 'true',
                'expanded' => 'true'
        ));
    }

    /**
     * @var integer $id
     *
     * @ORM\Column(name="campaign_os_id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    public function getId()
    {
        return $this->id;
    }

    /**
     * @ORM\ManyToOne( targetEntity="campaign", inversedBy="campaignOperatingSystems" )
     * @ORM\JoinColumn( name="campaign_os_id", referencedColumnName="campaign_id" )
     */
    private $campaign;

    public function setCampaign( \model\StoreBundle\Entity\campaign $ourCampaign )
    {
        $this->$campaign = $ourCampaign;
    }

    public function getCampaign()
    {
        return $this->$campaign;
    }

    /**
     * @ORM\OneToOne( targetEntity="listOS" )
     * @ORM\JoinColumn( name="campaign_os_id", referencedColumnName="os_id" )
     */
    private $os;

    public function setOS( \model\StoreBundle\Entity\listOS $campaignOS )
    {
        $this->os = $campaignOS;
    }

    public function getOS()
    {
        return $this->os;
    }
}

广告系列摘要:

/**
 * model\StoreBundle\Entity\campaign
 *
 * @ORM\Table(name="campaign")
 * @ORM\Entity
 */
class campaign extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('Name', 'text', array( 'label' => 'Campaign Name' ));
        //$builder->add('CampaignDescription', 'textarea', array( 'label' => 'Campaign Description' ));
        $builder->add('CampaignOperatingSystems', new campaignOS(), array( 'label' => 'Campaign OS' ));
/*      $builder->add('CampaignOperatingSystems', 'entity', array(
                'class' => 'modelStoreBundle:campaignOS',
                'property' => 'OS.OsName',
                'multiple' => 'true',
                'expanded' => 'true',
                'label' => 'Campaign OS'
        )); */
    }


    /**
     * @var integer $id
     *
     * @ORM\Column(name="campaign_id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    public function __construct()
    {
        $this->campaignOperatingSystems = new ArrayCollection();
    }
    /**
     * @ORM\OneToMany(targetEntity="campaignOS", mappedBy="campaign")
     * * , cascade={"persist", "remove"})
     * @var ArrayCollection $campaignOperatingSystems
     */
    protected $campaignOperatingSystems;

    public function addCampaignOperatingSystem(\model\StoreBundle\Entity\campaignOS $campaignOperatingSystems)
    {
        $this->campaignOperatingSystems[] = $campaignOperatingSystems;

        /* foreach( $campaignOperatingSystems as $campaignOS )
        {
            $campaignOS->setCampaign( $this );
        } */
        return $this;
    }

    public function removeCampaignOperatingSystem(\model\StoreBundle\Entity\campaignOS $campaignOperatingSystems)
    {
        $this->campaignOperatingSystems->removeElement($campaignOperatingSystems);
    }

    public function getCampaignOperatingSystems()
    {
        return $this->campaignOperatingSystems;
    }
}

这是显示和持续的内容:

public function newAction( Request $request )
    {

        //create a campaign
        $campaign = new campaign();

        // get the form
        $form = $this->createForm( new campaign(), $campaign );

        if ($request->getMethod() == 'POST') {
            $form->bindRequest($request);

            if ($form->isValid()) {
                // perform some action, such as saving the task to the database
                //get our data manager
                $em = $this->getDoctrine()->getManager();

                //make it presistent
                $em->persist($campaign);

                //update the DB
                $em->flush();

                return $this->redirect($this->generateUrl('displayDB'));
            }
        }

        return $this->render('modelStoreBundle:DisplayDB:newCampaign.html.twig', array(
                'form' => $form->createView(),
        ));

    }

这是堆栈跟踪: 堆栈跟踪

in /vagrant/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php at line 753   
            $state = $this->getEntityState($entry, self::STATE_NEW);
            if ( ! ($entry instanceof $assoc['targetEntity'])) {
                throw new ORMException(
                    sprintf(
                        'Found entity of type %s on association %s#%s, but expecting %s',
                        get_class($entry),
at UnitOfWork ->computeAssociationChanges (array('fieldName' => 'campaignOperatingSystems', 'mappedBy' => 'campaign', 'targetEntity' => 'model\StoreBundle\Entity\campaignOS', 'cascade' => array(), 'orphanRemoval' => false, 'fetch' => '2', 'type' => '4', 'inversedBy' => null, 'isOwningSide' => false, 'sourceEntity' => 'model\StoreBundle\Entity\campaign', 'isCascadeRemove' => false, 'isCascadePersist' => false, 'isCascadeRefresh' => false, 'isCascadeMerge' => false, 'isCascadeDetach' => false), object(PersistentCollection)) 

in /vagrant/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php at line 651    
at UnitOfWork ->computeChangeSet (object(ClassMetadata), object(campaign)) 

in /vagrant/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php at line 383    
at UnitOfWork ->computeScheduleInsertsChangeSets () 

in /vagrant/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php at line 673    
at UnitOfWork ->computeChangeSets () 

in /vagrant/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php at line 273    
at UnitOfWork ->commit (null) 

in /vagrant/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php at line 355    
at EntityManager ->flush (null) 

in kernel.root_dir/cache/dev/jms_diextra/doctrine/EntityManager.php at line 362    
at EntityManager ->flush () 

in /vagrant/src/model/StoreBundle/Controller/DisplayDBController.php at line 65    
at DisplayDBController ->newAction (object(Request)) 

1 个答案:

答案 0 :(得分:0)

这些方法的签名

public function addCampaignOperatingSystem(\model\StoreBundle\Entity\campaignOS $campaignOperatingSystems)
public function removeCampaignOperatingSystem(\model\StoreBundle\Entity\campaignOS $campaignOperatingSystems)

表示您希望在输入中输入 campaignOS 类型的单个对象,但是您要传递给其中一个ArrayCollection(campaignOS,我想)。

所以错误是签名或 - 可能 - 进入你的逻辑。

也许你想要像setCompaignOperationSystem这样的操作?