我得到的错误是:
在关联模型\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))

最佳答案

这些方法的签名

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

表示在输入中,您期望campaidos类型的单个对象,但您要传递给其中一个arrayCollection(我想是campaignOS)。
所以错误在于签名或者——可能是——你的逻辑。
也许你想做一个像这样的手术?

关于php - 在关联(数组)上找到类型为Doctrine\Common\Collections\ArrayCollection的实体,但期望(其他),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13561400/

10-12 16:44