无法在twig中从多个代理实体打印出实体ID

时间:2014-02-26 02:34:40

标签: symfony proxy doctrine entity

我希望有人可以帮助我,也许这是一个错误或有解决方案..

我有一个与客户实体具有ManyToOne关系的作业实体。

class Job {
 /**
 * @ORM\ManyToOne(targetEntity="Client")
 */
protected $client;}

当我从控制器获取客户端时

$client = $job->getClient();
// I could get client id here in controller, but could not render in twig template
$clientId = $client->getId(); 
return $this->render('client' => $client);

并在模板中呈现

{{ client.id|default('0') }}

始终输出0,

但客户端实体的所有其他属性都可以正确输出。

{{ client.name }}   ----> this can print out properly, except {{ client.id }}

我使用Netbeans调试窗口,我看到这个客户端实体是一个代理实体,它的__isInitialized__值是0,如果我设置:

$real_client = $em->getRepository('AceCoreBundle:Client')->find(4);

这是真实的实体,可以在树枝中输出客户端ID。

即使我有一个__isInitialized__值为1的代理实体,它也可以在树枝中输出实体的id值。

不确定它有什么问题..

由于这个问题,每次我必须渲染'client_id'=> $ clientId分别为twig

return $this->render(array('client_id' => $clientId, 'client' => $client));


{{ client_id|default('0') }}  // in twig

无论如何我可以将代理实体转换为真实实体吗?

My Client实体代码的一部分如下:

Client.php

// doctrine
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity()
 * @ORM\Table(name="client")
 * @Gedmo\SoftDeleteable(fieldName="deletedAt")
 */
class Client {

    const STATUS_NEW = 0; // new client
    const STATUS_APPROVED = 1;  // client approved by accounting team

    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */

    public $id;

    /*
     * @ORM\OneToOne(targetEntity="\Acme\UserBundle\Entity\User")
     *
      //protected $user; */

    /**
     * @ORM\OneToOne(targetEntity="Contact")
      @ORM\JoinColumn(name="primary_contact_id", referencedColumnName="id")
     */
    protected $primaryContact;

    /**
     * @ORM\OneToMany(targetEntity="Contact", mappedBy="client")
     */
    protected $contacts;

    /**
     * @ORM\OneToMany(targetEntity="\Acme\QuoteBundle\Entity\Costing", mappedBy="client")
     */
    protected $costings;

    /**
     * @ORM\OneToMany(targetEntity="\Acme\JobBundle\Entity\Job", mappedBy="client")
     */
    protected $jobs;

    /**
     * @ORM\Column(length=20, nullable=true)
     */
    protected $code;

    /**
     * @ORM\Column(name="display_name",length=150,unique=true)
     */
    protected $displayName;

    /**
     * @ORM\Column(name="created_at", type="datetime")
     */
    protected $createdAt;

    /**
     * @ORM\Column(name="delivery_addr",type="text",nullable=true)
     */
    protected $deliveryAddr;

    /**
     * @ORM\Column(length=200, nullable=true)
     * @Assert\NotBlank()
     */
    protected $street;

    /**
     * @ORM\Column(length=50, nullable=true)
     */
    protected $suburb;

    /**
     * @ORM\Column(length=50, nullable=true)
     */
    protected $city;

    /**
     * @ORM\Column(length=4, nullable=true)
     * @Assert\Length(
     *      min = "0",
     *      max = "4",
     *      minMessage = "",
     *      maxMessage = "The postcode cannot be longer than than {{ limit }} characters length"
     * )
     */
    protected $postcode;

    /**
     * @ORM\Column(name="courier_post_aid", type="integer")
     */
    protected $courierPostAid;

    /**
     * @ORM\Column(name="invoice_addr",type="text", nullable=true)
     */
    protected $invoiceAddr;

    /**
     * @ORM\ManyToOne(targetEntity="Acme\CoreBundle\Entity\Staff")
     * @ORM\JoinColumn(name="account_manager", referencedColumnName="id")
     */
    protected $accountManager;

    /**
     * @ORM\Column(type="smallint")
     */
    protected $status;

    /**
     * @ORM\Column(type="boolean",name="is_reseller", nullable=true)
     */
    protected $isReseller;

    /**
     * @ORM\Column(name="deleted_at", type="datetime", nullable=true)
     */
    protected $deletedAt;

/**
 * CONSTRUCTER OF CLASS
 */
public function __construct() {
    $this->contacts = new ArrayCollection();
    $this->status = $this::STATUS_NEW;
    $this->createdAt = new \DateTime();
    $this->isReseller = false;
    $this->courierPostAid = 0;
}

部分工作实体代码:

Job.php

// doctrine
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * @ORM\Entity(repositoryClass="Acme\JobBundle\Repository\JobRepository")
 * @ORM\Table(name="job")
 * @Gedmo\Loggable
 * @Gedmo\SoftDeleteable(fieldName="deletedAt")
 */
class Job {

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

    /**
     * @ORM\Column(type="integer", name="prev_job_id")
     */
    private $prevJobId;

    /**
     * @ORM\ManyToOne(targetEntity="Acme\CoreBundle\Entity\Category")
     */
    protected $category;

    /**
     * @ORM\Column(name="job_num",length=20)
     */
    protected $jobNum;

    /**
     * @ORM\Column(name="prev_job_num",length=20, nullable=true)
     */
    protected $prevJobNum;

    /**
     * @ORM\Column(name="costing_num",length=20)
     */
    protected $costingNum;

    /**
     * @ORM\ManyToOne(targetEntity="\Acme\CoreBundle\Entity\Client", inversedBy="jobs")
     */
    protected $client;

    /**
     * @ORM\Column(name="contact_id",type="smallint", nullable=true)
     */
    protected $contactId;

    /**
     * @ORM\Column(length=50, name="contact_firstname")
     */
    protected $contactFirstName;

    /**
     * @ORM\Column(length=50, name="contact_lastname", nullable=true)
     */
    protected $contactLastName;

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

    /**
     * @ORM\Column(name="special_instruction", type="text", nullable=true)
     * @Gedmo\Versioned
     */
    protected $specialInstruction;

    /**
     * @ORM\Column(name="date_in", type="datetime")
     */
    protected $dateIn;

    /**
     * @ORM\Column(name="date_out", type="datetime")
     */
    protected $dateOut;

    /**
     * @ORM\ManyToOne(targetEntity="Acme\CoreBundle\Entity\Staff")
     * @ORM\JoinColumn(name="account_manager", referencedColumnName="id")
     */
    protected $accountManager;

    /**
     * @ORM\Column(name="job_data", type="array")
     * @Gedmo\Versioned
     */
    protected $jobData;

    /**
     * @ORM\Column(name="created_at", type="datetime")
     */
    protected $createdAt;

    /**
     * @ORM\ManyToOne(targetEntity="Acme\CoreBundle\Entity\Staff")
     * @ORM\JoinColumn(name="created_by", referencedColumnName="id") 
     */
    protected $createdBy;

    /**
     * @ORM\Column(name="modified_at", type="datetime", nullable=true)
     */
    protected $modifiedAt;

    /**
     * @ORM\ManyToOne(targetEntity="Acme\CoreBundle\Entity\Staff")
     * @ORM\JoinColumn(name="modified_by", referencedColumnName="id") 
     */
    protected $modifiedBy;

    /**
     * @ORM\Column(name="deleted_at", type="datetime", nullable=true)
     */
    protected $deletedAt;

    // CONSTRUCTION
    public function __construct() {
        $this->createdAt = new \DateTime();
        $this->dateIn = new \DateTime();
        $this->prevJobId = 0;
    }

JobController.php

public function editAction($id) {

        $em = $this->getDoctrine()->getManager();
        $request = $this->getRequest();
        $job = $em->getRepository('AcmeJobBundle:Job')->find($id);



        if (!$job) {
            throw $this->createNotFoundException('Unable to find job entity.');
        }



        $client = $job->getClient();

        return array(
            'client' => $client,
        );
        }

edit.html.twig

<input type=hidden id="client-id" value="{{ client.id }}" name="client_id">

我尝试使用NetBeans来调试它,我发现当我更改app / cache / dev / classes.php时,关于4867行,在函数getAttribute中,我在下面添加了一些代码

// if I add this change to , it can print out id properly...
                    if ($item == 'id') {
                        return $object->getId();
                    }

之前:

 return $object->$item;

然后我可以在树枝上打印出id属性..

Netbean debug windows

1 个答案:

答案 0 :(得分:0)

我最好的猜测是,这是由于学说延迟加载客户端实体。您应该尝试将控制器的doctrine调用切换到具有客户端连接查询的存储库方法(如果需要,请不要忘记使用paginator)。或者在您的实体上切换默认提取模式(我不记得确切的步骤)。

仍然是一个奇怪的错误,请发布您的客户实体代码以获得更好的帮助。