我一直收到错误
Method "id" for object "AppBundle\Entity\Domains" does not exist in main\dashboard.html.twig at line 15
这是代码
主\ dashboard.html.twig
{% for domain in domains %}
<p> {{ domain.id }}</p>
<p> {{ domain.domain }}</p>
{% endfor %}
的appbundle \实体\ Domains.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Domains
* @ORM\Entity
* @ORM\Table(name="domains")
*/
class Domains
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
* @ORM\Column(type="string", length=500)
*/
protected $domain;
}
DashboardController.php
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity\Domains;
class DashboardController extends Controller
{
/**
* @Route("/", name="homepage")
*/
public function indexAction()
{
$domain = $this->getDoctrine()
->getRepository('AppBundle:Domains');
$domains = $domain->findAll();
foreach ($domains as $domain) {
dump($domain);
}
dump($domains);
// replace this example code with whatever you need
return $this->render('main/dashboard.html.twig', array('domains' => $domains));
}
}
现在我所知道的是$ domains肯定会得到这些条目。 E.g
Array ( [0] => AppBundle\Entity\Domains Object ( [id:protected] => 1 [domain:protected] => hello.com ) [1] => AppBundle\Entity\Domains Object ( [id:protected] => 2 [domain:protected] => stackoverflow.com ) )
而Twig肯定会收到'域',因为当我将假变量放入Twig时,它告诉它无法找到它。如果我删除domain.id,那么它将在domain.domain上出错。
我尝试在Twig中使用调试和转储来查看'域'的格式化方式,但我似乎无法在Symfony中工作。
谁能看到我哪里出错了?我认为它必须是我将$域传递给Twig的方式,或者是我尝试访问Twig中的域的方式。
答案 0 :(得分:0)
您需要生成getter / setter,如下所述:http://symfony.com/doc/current/book/doctrine.html#generating-getters-and-setters