Doctrine 2从相关实体获取项目

时间:2016-09-01 06:58:35

标签: orm doctrine-orm associations entity nette

我有一个包含ManyToOne,OneToOne关系的实体。我有另一个实体,它链接到第一个实体,我想要做的是获取有关第一个实体以及与之相关的所有其他实体的信息。

由于某种原因,当我进行调用时,它在相关实体中返回NULL。

代码:

<?php

namespace App\Model\Entities;

use Doctrine\ORM\Mapping as ORM,
    Doctrine\Common\Collections\ArrayCollection;
use Kdyby\Doctrine\Entities\BaseEntity;

/**
 * Doctrine entity for resorts in destinations
 * @package App\Model\Entities
 * @ORM\Entity
 */
class Resort extends BaseEntity
{

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

    /**
     * resort name
     * @ORM\Column(type="string")
     */
    protected $title;

    /**
     * @ORM\OneToOne(targetEntity="ResortProperties", mappedBy="resort", cascade={"persist"})
     */
    private $properties;

    public function __construct()
    {
        $this->properties = new ArrayCollection();
    }

    /**
     * HERE I WANT TO GET DATA FROM ANOTHER ENTITY
     * @return type
     */
    public function getProperties()
    {
        return $this->properties;
    }

}

相关实体是这样的:

<?php

namespace App\Model\Entities;

use Doctrine\ORM\Mapping as ORM,
    Doctrine\Common\Collections\ArrayCollection;
use Kdyby\Doctrine\Entities\BaseEntity;

/**
 * Doctrine entity for resort properties
 * @package App\Model\Entities
 * @ORM\Entity
 */
class ResortProperties extends BaseEntity
{

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

    /**
     * resort id
     * @ORM\OneToOne(targetEntity="Resort", inversedBy="Resort", cascade={"persist", "remove"})
     * @ORM\JoinColumn(onDelete="CASCADE")
     */
    protected $resort;

    /**
     * parameter1
     * @ORM\Column(type="string")
     */
    protected $parameter1;
}

我预料到,当我打电话给$repository->findAll();时,我会得到所有度假村实体,并且Resort->properties会加入ResortProperty实体,但它是NULL。

我不知道我做错了什么,有人能指出我的错误吗?感谢

2 个答案:

答案 0 :(得分:1)

我认为问题是JoinColumn-Annotation。 具有外键的实体(ResortProperties)需要知道如何加入这两个表。 然后,Resort-Entity上的&#34; inversedBy&#34; -Statement使用此规则。

它应该在您的ResortProperties-Entity中显示如下内容:

    /**
     * resort id
     * @ORM\OneToOne(targetEntity="Resort", inversedBy="Resort", cascade={"persist", "remove"})
     * @ORM\JoinColumn(name="resort_id", referencedColumnName="id")
     */
    protected $resort;

&#34; resort_id&#34;是您的外键列的名称。

希望这有帮助。

答案 1 :(得分:0)

<?php namespace MyApp; use Ratchet\MessageComponentInterface; use Ratchet\ConnectionInterface; class Chat implements MessageComponentInterface { protected $clients=array(); public function onOpen(ConnectionInterface $conn) { # get the cookies $cookies=(string)$conn->WebSocket->request->getHeader('Cookie'); # Returns only PHPSESSID (not SiteUser). //var_dump($cookies);exit; $this->clients[$conn->resourceId]=$conn; echo "New connection! ({$conn->resourceId})\n"; } /** * @param ConnectionInterface $conn * @param string $data */ public function onMessage(ConnectionInterface $conn, $data) { $database=$this->dbh; $data=json_decode($data, TRUE); if(isset($data['data']) && count($data['data'])!=0) { require_once BASE_PATH.'vendor'.DS.'autoload.php'; $memcache=new Memcache; $memcache->connect(DOMAIN_NAME, 11211); $storage=new NativeSessionStorage(array(), new Handler\MemcacheSessionHandler($memcache)); $session=new SymfonySession($storage); $type=$data['type']; $user_id=$conn->Session->get('user_id'); $return=NULL; if($type=="send" && isset($data['data']['type']) && $user_name!=-1) { $msg=htmlspecialchars($data['data']['msg']); $date=new DateTime; $date->setTimezone(new DateTimeZone(TIMEZONE)); if($data['data']['type']=='text') { echo 'test 4';exit; $database->query('SELECT `id`, `user_id`, `msg`, `type` FROM `chat` ORDER BY `id` DESC LIMIT 1', array()); $lastMsg=$database->statement->fetch(PDO::FETCH_OBJ); if($lastMsg->user_id==$user_id && (strlen($lastMsg->msg)<=100 || strlen($lastMsg->msg)+strlen($msg)<=100)) { # Append message. $msg=$lastMsg->msg."<br/>".$msg; $database->query('UPDATE `chat` SET `msg`=:msg, `posted`=NOW() WHERE `id`=:lastmsg', array( ':msg'=>$msg, ':lastmsg'=>$lastMsg->id )); $return=array( "id"=>$lastMsg->id, "name"=>$user_name['staffname'], "type"=>"text", "msg"=>$msg, "posted"=>$date->format("Y-m-d H:i:sP"), "append"=>TRUE ); } else { $database->query('INSERT INTO `chat` (`user_id`, `msg`, `type`, `posted`) VALUES (?, ?, "text", NOW())', array( $user_id, $msg )); # Get last insert ID. $get_chat_id=$database->lastInsertId(); $return=array( "id"=>$get_chat_id, "name"=>$user_name['staffname'], "type"=>"text", "msg"=>$msg, "posted"=>$date->format("Y-m-d H:i:sP") ); } } foreach($this->clients as $client) { $this->send($client, "single", $return); } } elseif($type=="fetch") { # Fetch previous messages. $this->fetchMessages($conn, $data['data']['id']); } } } } 实体中,我必须进行此更改:

ResortProperties

class ResortProperties extends BaseEntity { /** * resort id * @ORM\OneToOne(targetEntity="Resort", inversedBy="properties", cascade={"persist", "remove"}) * @ORM\JoinColumn(name="resort_id", referencedColumnName="id", onDelete="CASCADE") */ protected $resort; } 实体中,我必须改变它:

Resort

现在它正常工作,希望这会有助于其他人