我希望有人能幫助我,也許這是一個bug或者有它的解決方案..無法在樹枝從許多打印出實體ID到一個代理實體
我有具有多對一關係的作業實體到客戶實體。
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到小枝
return $this->render(array('client_id' => $clientId, 'client' => $client));
{{ client_id|default('0') }} // in twig
反正是有我可以將代理實體轉換爲真實體?
部分的下面我的客戶實體代碼:
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調試它,我發現,當我更改應用程序/緩存的/ 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屬性..
這絕對是奇怪的,我不認爲這是一個代理實體或類似的問題。奇怪的是,你可以得到除id之外的所有屬性...檢查數據庫中id的實際值。如果您不設置過濾器默認值,會得到什麼錯誤信息?如果你只寫{{client.id}},你會得到一個錯誤嗎? – Cesc
如果我不放置默認值,它沒有任何錯誤信息,它只是打印出空值。如,它會打印出 –
我已經在問題的末尾發佈了部分代碼。請看一看。謝謝。 –