Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Doctrine2和cascade persist-调用非对象上的成员函数getId()_Php_Symfony_Doctrine Orm_Cascade - Fatal编程技术网

Php Doctrine2和cascade persist-调用非对象上的成员函数getId()

Php Doctrine2和cascade persist-调用非对象上的成员函数getId(),php,symfony,doctrine-orm,cascade,Php,Symfony,Doctrine Orm,Cascade,我正在测试Symfony2和Doctrin2中作业实体和类别实体之间的级联持久性。 这是我的类别实体: <?php namespace Ibw\JobeetBundle\Entity; use Doctrine\Common\Collections\ArrayCollection; use Ibw\JobeetBundle\Entity\Affiliate; use Ibw\JobeetBundle\Entity\Job; use Ibw\JobeetBundle\Utils\Jobee

我正在测试Symfony2和Doctrin2中
作业
实体和
类别
实体之间的级联持久性。 这是我的
类别
实体:

<?php
namespace Ibw\JobeetBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Ibw\JobeetBundle\Entity\Affiliate;
use Ibw\JobeetBundle\Entity\Job;
use Ibw\JobeetBundle\Utils\Jobeet;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="Ibw\JobeetBundle\Repository\CategoryRepository")
 * @ORM\Table(name="categories")
 * @ORM\HasLifecycleCallbacks
 */
class Category 
{

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=255, unique=true)
     */
    protected $name;

    /**
     * @ORM\OneToMany(targetEntity="Job", mappedBy="category", cascade={"persist"})
     */
    protected $jobs;

    /**
     * @ORM\ManyToMany(targetEntity="Affiliate", mappedBy="categories")
     */
    protected $affiliates;

    /**
     * @ORM\Column(type="string", length=255, unique=true)
     */
    protected $slug;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->jobs = new ArrayCollection();
        $this->affiliates = new ArrayCollection();
    }

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

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

        return $this;
    }

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

    /**
     * Add jobs
     *
     * @param Job $jobs
     * @return Category
     */
    public function addJob(Job $jobs)
    {
        $this->jobs[] = $jobs;

        return $this;
    }

    /**
     * Remove jobs
     *
     * @param Job $jobs
     */
    public function removeJob(Job $jobs)
    {
        $this->jobs->removeElement($jobs);
    }

    /**
     * Get jobs
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getJobs()
    {
        return $this->jobs;
    }

    /**
     * Add affiliates
     *
     * @param Affiliate $affiliates
     * @return Category
     */
    public function addAffiliate(Affiliate $affiliates)
    {
        $this->affiliates[] = $affiliates;

        return $this;
    }

    /**
     * Remove affiliates
     *
     * @param Affiliate $affiliates
     */
    public function removeAffiliate(Affiliate $affiliates)
    {
        $this->affiliates->removeElement($affiliates);
    }

    /**
     * Get affiliates
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getAffiliates()
    {
        return $this->affiliates;
    }

    /**
     * Set slug
     *
     * @param string $slug
     * @return Category
     */
    public function setSlug($slug)
    {
        $this->slug = $slug;

        return $this;
    }

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

    /**
     * @ORM\PrePersist
     * @ORM\PreUpdate
     */
    public function setSlugValue()
    {
        $this->setSlug(Jobeet::slugify($this->getName()));
    }
}
现在,当我尝试使用此测试对其进行测试时:

public function testAddJobToCategory()
    {
        $job = new Job();
         $job->setType('flexible-time');
         $job->setCompany('Sensio Labs');
         $job->setLogo('sensio-labs.gif');
         $job->setUrl('http://www.sensiolabs.com/');
         $job->setPosition('Web Developer');
         $job->setLocation('Paris, France');
         $job->setDescription('You\'ve already developed websites with symfony and you want to work with Open-Source technologies. You have a minimum of 3 years experience in web development with PHP or Java and you wish to participate to development of Web 2.0 sites using the best frameworks available.');
         $job->setHowToApply('Send your resume to fabien.potencier [at] sensio.com');
         $job->setIsPublic(true);
         $job->setIsActivated(true);
         $job->setToken('job');
         $job->setEmail('job@example.com');
         $job->setExpiresAt(new \DateTime('+30 days'));

         $category = $this->em->createQueryBuilder('c')
                        ->select('c')
                        ->from('IbwJobeetBundle:Category', 'c')
                        ->where('c.id = 1')
                        ->getQuery()
                        ->getSingleResult();

        $category->addJob($job);
        $this->em->persist($category);
        $this->em->flush();

        $jobFromQuery = $this->em->createQueryBuilder('j')
                    ->select('j')
                    ->from('IbwJobeetBundle:Job', 'j')
                    ->where('j.type = :type')
                    ->setParameter('type', 'flexible-time')
                    ->setMaxResults(1)
                    ->setFirstResult(1)
                    ->getQuery()
                    ->getSingleResult();


        $this->assertEquals(1, $jobFromQuery->getCategory()->getId());
    }
运行此测试时,出现以下错误:

PHP致命错误:对非对象调用成员函数getId()

所以我认为它在
$jobFromQuery->getCategory()->getId()
部分
getCategory()
没有设置或是其他什么。

这个条令文件说:

条令只会检查关联的拥有方是否有变更

要完全理解这一点,请记住如何在对象世界中维护双向关联。关联的每一侧有2个引用,这2个引用都表示相同的关联,但可以相互独立地更改。当然,在正确的应用程序中,双向关联的语义由应用程序开发人员正确维护(这是他的责任)。条令需要知道这两个内存引用中哪一个应该持久化,哪一个不应该持久化。这就是拥有/反向概念的主要用途

仅对关联的反面所做的更改将被忽略。确保更新双向关联的双方(或者至少从原则的角度更新拥有方)

双向关联的拥有方是在确定关联状态时“关注”的一方,因此也就是在数据库中是否需要更新关联


在您的代码中,您只需执行
$category->addJob($job)因此您正在反向设置关联。作为被忽略的文档,您至少应该首先执行
$job->setCategory($category)

Hmmm,我可以看到
$job->setCategory()
解决了所有问题。但是,
$category->addJob()
有什么用呢?如果你想查看一个类别的每一个作业,那么只用于对象引用,而对于条令来说,相反的一面并不重要
public function testAddJobToCategory()
    {
        $job = new Job();
         $job->setType('flexible-time');
         $job->setCompany('Sensio Labs');
         $job->setLogo('sensio-labs.gif');
         $job->setUrl('http://www.sensiolabs.com/');
         $job->setPosition('Web Developer');
         $job->setLocation('Paris, France');
         $job->setDescription('You\'ve already developed websites with symfony and you want to work with Open-Source technologies. You have a minimum of 3 years experience in web development with PHP or Java and you wish to participate to development of Web 2.0 sites using the best frameworks available.');
         $job->setHowToApply('Send your resume to fabien.potencier [at] sensio.com');
         $job->setIsPublic(true);
         $job->setIsActivated(true);
         $job->setToken('job');
         $job->setEmail('job@example.com');
         $job->setExpiresAt(new \DateTime('+30 days'));

         $category = $this->em->createQueryBuilder('c')
                        ->select('c')
                        ->from('IbwJobeetBundle:Category', 'c')
                        ->where('c.id = 1')
                        ->getQuery()
                        ->getSingleResult();

        $category->addJob($job);
        $this->em->persist($category);
        $this->em->flush();

        $jobFromQuery = $this->em->createQueryBuilder('j')
                    ->select('j')
                    ->from('IbwJobeetBundle:Job', 'j')
                    ->where('j.type = :type')
                    ->setParameter('type', 'flexible-time')
                    ->setMaxResults(1)
                    ->setFirstResult(1)
                    ->getQuery()
                    ->getSingleResult();


        $this->assertEquals(1, $jobFromQuery->getCategory()->getId());
    }