0
我post.php中爲什麼連接表在symfony2中不起作用?
<?php
// src/Acme/UserBundle/Entity/User.php
namespace Acme\PostBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity
* @ORM\Table(name="posts")
*/
class Post
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
*
* @ORM\Column(type="integer")
*/
public $user_id;
/**
* @ORM\ManyToOne(targetEntity="Acme\PostBundle\Entity\User", inversedBy="posts")
* @ORM\JoinColumn(name="id", referencedColumnName="id")
*/
protected $user;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank(message="Введите текст")
*)
*/
protected $text;
/**
* @ORM\Column(type="string", length=255)
*/
protected $address;
/**
*
* @ORM\Column(type="datetime")
*/
protected $date;
}
而且我user.php的:
<?php
// src/Acme/UserBundle/Entity/User.php
namespace Acme\PostBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity
* @ORM\Table(name="users")
*/
class User
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
*
* @ORM\Column(type="string")
*/
protected $path;
/**
* @ORM\OneToMany(targetEntity="Acme\PostBundle\Entity\Post", mappedBy="users")
*/
protected $posts;
public function __construct() {
$this->posts = new ArrayCollection();
}
}
我想通過做表 '崗位' 列 'user_ID的' 連接表用戶和帖子。接下來我讓查詢中使用的QueryBuilder:
$repository = $this->getDoctrine()->getManager()->getRepository('AcmePostBundle:Post');
$queryPosts = $repository->createQueryBuilder('p')
->orderBy('p.id', 'DESC')
->getQuery();
return new Response(var_dump($queryPosts->getResult()));
而我得到:
object(Acme\PostBundle\Entity\Post)[476]
protected 'id' => int 1
public 'user_id' => int 1
protected 'user' =>
object(Proxies\__CG__\Acme\PostBundle\Entity\User)[475]
public '__initializer__' =>
object(Closure)[469]
...
public '__cloner__' =>
object(Closure)[470]
...
public '__isInitialized__' => boolean false
protected 'id' => int 1
protected 'path' => null
protected 'posts' => null
protected 'text' => string 'Test' (length=4)
protected 'address' => string 'Test' (length=4)
protected 'date' =>
object(DateTime)[477]
public 'date' => string '2014-11-06 14:39:13' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/Kiev' (length=11)
爲什麼用戶>路徑爲空?如何使使這個連接表
使用輔助功能時
它的工作,但現在我得到這個:http://pastebin.com/C9NUXcwT – engilexial 2014-11-06 18:23:30
@ivaaaan編輯你的問題,但是你應該做一個新的問題,因爲你有任何新問題是不相關的這一個。 – sjagr 2014-11-06 18:23:58
@ivaaaan其實,你的新輸出有什麼問題?有什麼問題? – sjagr 2014-11-06 18:25:07