Synfony 3学说显示了回购关系中的内容

时间:2017-08-15 07:09:49

标签: php symfony orm doctrine

我必须使用symfony实体SrvrsServersNcBackupEvents

  • NCbrtBundle:SrvrsServers(id,name,description等)

关系代码:

 /**
 * One SrvrsServers has Many NcBackupEvents.
 * @ORM\OneToMany(targetEntity="NcBackupEvents", mappedBy="SrvrsServers")
 */
private $ncBackupEvents;

public function __construct() {
    $this->ncBackupEvents = new ArrayCollection();
} 
  • NCbrtBundle:NcBackupEvents(id,date,status等)

这是NcBackupEvents

 /**
 * @var \SrvrsServers
 *
 * @ORM\ManyToOne(targetEntity="SrvrsServers")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="srvrs_servers_id", referencedColumnName="id")
 * })
 */
private $srvrsServers;

SrvrsServersNcBackupEvents的关系是一对多。在NcBackupEvents的回购中我能够获取有关自身的信息。这有效,我认为SrvrsServers对象被拉了,但我不是100%肯定,也不知道如何访问它:

class NcBackupEventsRepository extends \Doctrine\ORM\EntityRepository
{
    public function findByServerBackup($parameters)
    {
        $dql = 'SELECT n FROM NCbrtBundle:NcBackupEvents n 
            WHERE n.backupmethod LIKE :backupmethod
            AND n.backupType LIKE :backup_type 
            AND n.log LIKE :log';
        $query = $this->getEntityManager()
                ->createQuery($dql)
                ->setParameter('backupmethod',
                    '%'.$parameters['backupmethod'].'%')
                ->setParameter('backup_type',
                    '%'.$parameters['backup_type'].'%')
                ->setParameter('log', '%'.$parameters['log'].'%');
       try {
            return $query->getResult();
        } catch (\Doctrine\ORM\NoResultException $e) {
            return null;
       }
    }
}

如何使用SrvrsServers->name字段来实现Controller的QUERY?我尝试过很多东西,似乎没什么用。这应该很简单,但我不明白如何完成它。我试图遵循official docs并且由于某种原因无法使其工作。有人能指出我这样做的方法吗?请保持在场,我是新手。

编辑:SAMPLE Con​​troller。

 /**
 * @Route("/")
 */
public function indexAction()
{
    $paramaters = array('backupmethod' => 't-py',
        'backup_type' => '', 'log' => 'fuc');
    $em = $this->getDoctrine()
            ->getRepository('NCbrtBundle:NcBackupEvents')
            ->findByServerBackup($paramaters);
    foreach($em as $value){
        print_r(gettype($value));
        print_r($value->getSrvrsServers()->getName());
    }
    return $this->render('NCbrtBundle:Default:index.html.twig');
}

EDIT2:我想将服务器name包含在$paramaters实体的SrvrsServers变量中

0 个答案:

没有答案