如何使用学说协会/关系不起作用

时间:2016-12-30 16:38:33

标签: symfony doctrine-orm

我试图通过这个http://symfony.com/doc/current/doctrine/associations.html在交响乐中建立关系 问题是,当我发布并且不会获得相关权利(rss)时,我得到空

<?php

命名空间AppBundle \ Entity;

将Doctrine \ ORM \ Mapping用作ORM;

/**
 * Post
 * @ORM\Entity
 * @ORM\Table(name="post")
 */

class Post
{

/**
 * @var int
 */
private $id;

/**
 * @var string
 */
private $title;

/**
 * @var string
 */
private $text;


 /**
 * @ORM\ManyToOne(targetEntity="Rss", inversedBy="posts")
 * @ORM\JoinColumn(name="link", referencedColumnName="id")
 */
private $rss;

/**
 * @var \DateTime
 */
private $createdAt;



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

/**
 * Set title
 *
 * @param string $title
 *
 * @return Post
 */
public function setTitle($title)
{
    $this->title = $title;

    return $this;
}

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

/**
 * Set text
 *
 * @param string $text
 *
 * @return Post
 */
public function setText($text)
{
    $this->text = $text;

    return $this;
}

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

/**
 * Set link
 *
 * @param string $link
 *
 * @return Post
 */
public function setLink($link)
{
    $this->link = $link;

    return $this;
}

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

/**
 * Set createdAt
 *
 * @param \DateTime $createdAt
 *
 * @return Post
 */
public function setCreatedAt($createdAt)
{
    $this->createdAt = $createdAt;

    return $this;
}

/**
 * Get createdAt
 *
 * @return \DateTime
 */
public function getCreatedAt()
{
    return $this->createdAt;
}
/**
 * @var integer
 */
private $user_id;


/**
 * Set userId
 *
 * @param integer $userId
 *
 * @return Post
 */
public function setUserId($userId)
{
    $this->user_id = $userId;

    return $this;
}

/**
 * Get userId
 *
 * @return integer
 */
public function getUserId()
{
    return $this->user_id;
}
/**
 * @var integer
 */
private $link;


public function setRss($rss){

    $this->rss = $rss;        
}

public function getRss(){
    return $this->rss;
}


}

<?php

namespace AppBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * rss
 * 
 * @ORM\Entity
 * @ORM\Table(name="rss")
 */
class Rss
{

/**
 * @var int
 */
private $id;

/**
 * @var string
 */
private $name;

/**
 * @var string
 */
private $address;


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

/**
 * Set name
 *
 * @param string $name
 *
 * @return rss
 */
public function setName($name)
{
    $this->name = $name;

    return $this;
}

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

/**
 * Set address
 *
 * @param string $address
 *
 * @return rss
 */
public function setAddress($address)
{
    $this->address = $address;

    return $this;
}

/**
 * Get address
 *
 * @return string
 */
public function getAddress()
{
    return $this->address;
}
/**
 * @var \DateTime
 */
private $lastUpdate;


  /**
 * @ORM\OneToMany(targetEntity="Post", mappedBy="Rss")
 */
private $posts;


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

public function getPosts(){

    return $this->posts;
}

public function setPosts($posts){
    $this->posts[] = $posts;
}
/**
 * Set lastUpdate
 *
 * @param \DateTime $lastUpdate
 *
 * @return rss
 */
public function setLastUpdate($lastUpdate)
{
    $this->lastUpdate = $lastUpdate;

    return $this;
}

/**
 * Get lastUpdate
 *
 * @return \DateTime
 */
public function getLastUpdate()
{
    return $this->lastUpdate;
}
/**
 * @var string
 */
private $pageaddress;




/**
 * Set pageaddress
 *
 * @param string $pageaddress
 *
 * @return rss
 */
public function setPageaddress($pageaddress)
{
    $this->pageaddress = $pageaddress;

    return $this;
}

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


public function indexAction()
{


    $em = $this->getDoctrine()->getManager();

    $posts = $em->getRepository('AppBundle:Post')->findAll();
    $post = $this->getDoctrine()
        ->getRepository('AppBundle:Post')
    ->find(1);

    $post->getRss();

1 个答案:

答案 0 :(得分:0)

这可以帮到你:

https://knpuniversity.com/screencast/doctrine-relations/many-to-one-relation

您也可以尝试修改变量$ id

import asyncio


@asyncio.coroutine
def foo():
    print("enter foo")
    timers = []
    for i in range(10):
        print("Start foo", i)
        yield from asyncio.sleep(0.5)
        print("Got foo", i)
        timers.append(asyncio.ensure_future(timer(i)))
        yield
    print("foo waiting")
    # wait for all timers to finish
    yield from asyncio.wait(timers)
    print("exit foo")


@asyncio.coroutine
def timer(i):
    print("Setting timer", i)
    yield from asyncio.sleep(2)
    print("**** Timer", i)


loop = asyncio.get_event_loop()
resp = loop.run_until_complete(foo())
loop.close()

private $id

private $ id;

创建主键。

实体

课程帖子

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

类Rss

/**
 * @ORM\ManyToOne(targetEntity="Rss", inversedBy="post")
 * @ORM\JoinColumn(nullable=false)
 */
private $rss;

和Post类你需要创建一个这样的构造函数

/**
 * @ORM\OneToMany(targetEntity="Post", mappedBy="rss")
 * @ORM\OrderBy({"createdAt"="DESC"})
 */
private $post;

我希望它可以帮到你