Symfony Query原则实体

时间:2015-09-25 11:04:33

标签: php mysql symfony doctrine-orm

我在doctrine中遇到了一个问题,我最近开始使用Symfony继续在Symfony中继续一个旧项目,现在我想学习它。 我开始从db解释,我只写了我感兴趣的字段:

  • 用户(ID,姓名,柏)

  • user_reference(id,id_user [FOREIGN KEY id FROM user],id_user_referenced [FOREIGN KEY id FROM user])

这是查询:

     $id_user = $user->getId();
        $query = $em->createQueryBuilder()
     ->select('ur','uu')   
     ->from('DtEcBundle:UserReferences', 'ur')
     ->innerJoin("ur.id_user","uu")
     ->where("ur.id_user = :id_user")
     ->setParameter("id_user",$id_user)
            ->getQuery();
            $userpyramid = $query->getResult();

我在我的twig文件id_user_referenced中打印,但我也会打印"名称,姓氏和路径"来自USER表

对于Entity UserReferences中的print id_user_referenced,有以下代码:

    /**
 * Set id_user_referenced
 *
 * @param \Dt\EcBundle\Entity\User $idUserReferenced
 * @return UserReferences
 */
public function setIdUserReferenced(\Dt\EcBundle\Entity\User $idUserReferenced = null) {
    $this->id_user_referenced = $idUserReferenced;

    return $this;
}

/**
 * Get id_user_referenced
 *
 * @return \Dt\EcBundle\Entity\User 
 */
public function getIdUserReferenced() {
    return $this->id_user_referenced;
}

使用以下内容将实体用户中的id(如id)转换为字符串:

    public function __toString(){
    return strval($this->id);
}

现在,如果我在select this中添加我的查询:

->select('ur','uu.path')  

Symfony发给我一个错误:

  

Key" idUserReferenced"对于带有键的数组" 0,path"在...中不存在   DtEcBundle:Profilo:第40行的digitalpr-profile.html.twig

为什么?我怎么能解决它?

UserReferences.php

 namespace Dt\EcBundle\Entity;

 use Doctrine\ORM\Mapping as ORM;

 /**
 * UserReferences
 *
 * @ORM\Table(name="user_references" ,uniqueConstraints=         {@ORM\UniqueConstraint(name="recension_unique", columns={"id_user",      "id_user_referenced"})})
 * @ORM\Entity(repositoryClass="Dt\EcBundle\Entity\UserReferencesRepository")
 */  class UserReferences {
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 *
 * @var Dt\EcBundle\Entity\User
 * @ORM\ManyToOne(targetEntity="Dt\EcBundle\Entity\User",     inversedBy="references")
 * @ORM\JoinColumn(name="id_user", referencedColumnName="id")
 */
  private $id_user;

/**
 * @ORM\ManyToOne(targetEntity="Dt\EcBundle\Entity\User")
 * @ORM\JoinColumn(name="id_user_referenced", referencedColumnName="id")
 * */
private $id_user_referenced;

/**
 *
 * @var string
 * @ORM\Column(name="reference", type="text", nullable=false,unique=false);
 */
private $reference;

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

/**
 * Set reference
 *
 * @param string $reference
 * @return UserReferences
 */
public function setReference($reference) {
    $this->reference = $reference;

    return $this;
}

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

/**
 * Set id_user
 *
 * @param \Dt\EcBundle\Entity\User $idUser
 * @return UserReferences
 */
public function setIdUser(\Dt\EcBundle\Entity\User $idUser = null) {
    $this->id_user = $idUser;

    return $this;
}

/**
 * Get id_user
 *
 * @return \Dt\EcBundle\Entity\User 
 */
public function getIdUser() {
    return $this->id_user;
}

/**
 * Set id_user_referenced
 *
 * @param \Dt\EcBundle\Entity\User $idUserReferenced
 * @return UserReferences
 */
public function setIdUserReferenced(\Dt\EcBundle\Entity\User $idUserReferenced = null) {
    $this->id_user_referenced = $idUserReferenced;

    return $this;
}

/**
 * Get id_user_referenced
 *
 * @return \Dt\EcBundle\Entity\User 
 */
public function getIdUserReferenced() {
    return $this->id_user_referenced;
}
}

user.php的

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

/**
 * @var string
 * 
 * @ORM\Column(name="name", type="string", length=255, unique=false, nullable=false)  
 * @Assert\NotBlank(message="user.name.not.blank")
 * @Assert\Length(
 *          min=2,
 *          max=150,
 *          minMessage="user.name.not.min",
 *          maxMessage="user.name.not.max" )
 */
protected $name;

/**
 * @var string
 * 
 * @ORM\Column(name="surname", type="string", length=255, unique=false, nullable=false)  
 * @Assert\NotBlank(message="user.surname.not.blank")
 * @Assert\Length(
 *          min=2,
 *          max=150,
 *          minMessage="user.surname.not.min",
 *          maxMessage="user.surname.not.max" )
 */
protected $surname;

/**
 * @var \DateTime
 * @ORM\Column(name="borndate", type="datetime",unique=false,nullable=false) 
 */
protected $borndate;

/**
 *
 * @var string
 * @ORM\Column(name="tel", type="string",length=50, unique=true,nullable=true)
 * @Assert\Regex("/[0-9]/")
 */
protected $tel;

/**
 *
 * @var string
 * @ORM\Column(name="city", type="string",length=255,unique=false,nullable=true) 
 * @Assert\NotBlank(message="user.expert.city.not.blank",groups={"Expert"})
 * @Assert\NotBlank(message="user.expert.city.not.blank",groups={"ExpertProfile"})
 * @Assert\Length(
 *          min=2,
 *          max=150,
 *          minMessage="user.expert.city.not.min",
 *          maxMessage="user.expert.city.not.max", groups={"Expert"} )
 * @Assert\Length(
 *          min=2,
 *          max=150,
 *          minMessage="user.expert.city.not.min",
 *          maxMessage="user.expert.city.not.max", groups={"ExpertProfile"} )
 */
protected $city;

/**
 *
 * @var string
 * @ORM\Column(name="street", type="string",length=255,unique=false,nullable=true) 
 * @Assert\NotBlank(message="user.expert.street.not.blank",groups={"Expert"})
 * @Assert\NotBlank(message="user.expert.street.not.blank",groups={"ExpertProfile"})
 * @Assert\Length(
 *          min=2,
 *          max=150,
 *          minMessage="user.expert.street.not.min",
 *          maxMessage="user.expert.street.not.max", groups={"Expert"} )
 * @Assert\Length(
 *          min=2,
 *          max=150,
 *          minMessage="user.expert.street.not.min",
 *          maxMessage="user.expert.street.not.max", groups={"ExpertProfile"} )
 */
protected $street;

/**
 *
 * @var type
 * 
 * @Assert\File(
 *     maxSize = "1024k",
 *     mimeTypes = {"image/gif","image/jpeg","image/pjpeg","image/png"},
 *     mimeTypesMessage = "user.image.mimetypes",
 *     maxSizeMessage   = "user.image.maxsize"
 * )
 * @Assert\NotBlank(message="user.expert.mandatory.photo",groups={"Expert"})
 * 
 */
protected $photo;

/**
 * @ORM\Column(name="photo_path",type="string", length=255, nullable=true,unique=true)
 */
protected $path;
/**
 * Membri per la gestione dei file
 * 
 */

2 个答案:

答案 0 :(得分:1)

问题在于,学说会在Array Hydration模式下保持您的结果,而您期望Object Hydraion

在第一种情况下,您要求提供2个相关对象,并且学说可以链接它们。在第二个中你要求一个对象和一个标量值,并且教义不能链接它们,所以它将它们返回到结果数组的两个单独的字段中。

所以你在select('ur','uu.path')的情况下从$ query-> getResult()获得的结果不是User对象的数组,而是一个包含2个字段的数组 - $ result [0]所有找到的用户对象,以及uu.path的$ result [' path'] - 因为uu.path是标量值而不是对象。

因此,您需要制作select('ur','uu')并将结果视为$result->getIdUserReferenced()->getPath()

或者(如果你想节省一些资源,但我认为不值得),请制作select('ur','uu.path')然后制作var_dump结果。你会看到如何满足你的需求。

答案 1 :(得分:0)

我尝试这种模式,但不是很好

  $id_user = $user->getId();
        $query = $em->createQueryBuilder()
     ->select('ur','uu')   
     ->from('DtEcBundle:UserReferences', 'ur')
     ->innerJoin("ur.id_user","uu")
     ->where("ur.id_user = :id_user")
     ->setParameter("id_user",$id_user)
            ->getQuery();
            $userpyramid = $query->getResult();


    $form = $this->get('form.factory')->createNamedBuilder('form', 'form')
        ->setMethod('POST')
        ->setAction($this->generateUrl('profilo_secondlevel'))
        ->add('save', 'submit', ['label' => 'Prova'])
        ->getForm();*/


        $result->getIdUserReferenced()->getPath();

    return $this->render('DtEcBundle:Profilo:index.html.twig', array(
        'user'              => $user,
        'tags'              => $tags,
        'followers'         =>$followers,
        'expert'            =>$expert,
        'user_expert'       =>$user_expert,
        'well_cat'          =>$well_cat,
        'user_notification' => $user_notifications,
        "udputenti" => $udputenti,
        "userpyramid" => $userpyramid,
        "result"=> $result
        //'form'=>$form->createView()
    ));`