相关疑难解决方法(0)

symfony2:如何从模板访问服务

如果我创建了一个服务,有没有办法从twig访问它,而不创建twig.extension?

php symfony

43
推荐指数
1
解决办法
3万
查看次数

Symfony2中的Doctrine2:如何查看哪个对象调用导致查询?

我正在使用Symfony2和Doctrine2.对于我的项目,我使用不同的关联映射创建了实体.首先,我确实看到了大约7个查询请求一个对象,所以我决定"急切加载",它减少到三个查询.

但是他们中的两个看起来在symfony工具栏(Profiler)中是相同的,直接相互调用.根据我的理解,我的代码中不需要第三个查询.

那么在哪里我必须在doctrine php文件中设置断点,看看我的代码哪一行让doctrine调用一个新的查询?或者是否有其他解决方案来了解我如何优化此请求?

更新:

在考虑了Artworkad的回答后,我必须更详细地了解.这是因为我没有通过我的控制器发出2个对象请求.但也许它与我的树枝有关?

我的控制器

public function gebietAction($gebiet){
        $em = $this->getDoctrine()->getEntityManager();
        /* @var $gebietobj Gebiet */
        $gebietobj = $em->getRepository('ACGSigwxBundle:Gebiet')->findOneBy(array('short' => $gebiet));
        if (!$gebietobj) {
            throw $this->createNotFoundException('Kann das angegebene Gebiet nicht finden!');
        }
        return $this->render('ACGSigwxBundle:Sigwx:sigwx.html.twig',array("gebiet"=>$gebietobj));
    }
Run Code Online (Sandbox Code Playgroud)

我的树枝模板

{% extends "ACGSigwxBundle::layout.html.twig" %}

{% block content %}
    <h1>{{ gebiet.getName() }}</h1>
    <p>My sectors:</p>
    <ul>
    {% for gs in gebiet.getGebietssektoren() %}
        <li>{{ gs.getSektor().getName() }}</li>
    {% endfor %}
    </ul>
{% endblock %}
Run Code Online (Sandbox Code Playgroud)

对象关联

Gebiet n:n Sektor与属性有关联.所以我Gebiet 1:n Gebietsektoren n:1 Sektor使用标准[doctrine2 association …

php mysql debugging symfony doctrine-orm

6
推荐指数
1
解决办法
1845
查看次数

Symfony2/Doctrine如何在实体中存储相关对象的计数

我已经设置了一个包含测试对象的包,该对象包含许多testQuestion对象,每个对象都是一个问题和给定的答案(如果没有答案则为0).从树枝上我希望能够从测试对象中获取信息,说明有多少问题以及已经回答了多少问题.

我创建了一个查询来将其从数据库中拉出来,在测试实体中我创建了2个新属性来存储问题的数量和回答的数量.我创建了一个查询所在的TestRepository.Test对象检查对象是否设置了值,如果没有,则在需要时加载它,因为我不总是需要这些信息.

但是我仍然坚持如何将存储库代码链接到测试对象,既调用repo函数又调用repo函数将值保存到相关的Test对象.

ACME/Quizbundle /测试/ test.php的

namespace Acme\QuizBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Acme\QuizBundle\Entity\TestRepository;

/**
 * @ORM\Entity(repositoryClass="Acme\QuizBundle\Entity\TestRepository")
 * @ORM\Table(name="test")
 */
class Test    {
protected $numQuestions = null;
protected $numQuestionsAnswered = null;

public function getNumQuestionsAnswered () {
    if (is_null($this->numQuestionsAnswered)) {
        $repository = $this->getEntityManager()->getRepository('\AcmeQuizBundle\Test');
        $values = $repository->calculateNumQuestions();
    }
    return $this->numQuestionsAnswered;
}
Run Code Online (Sandbox Code Playgroud)

Acme/Quizbundle/Test/TestRepository.php(有一个getNumQuestions()的匹配方法)

namespace Acme\QuizBundle\Entity;

use Doctrine\ORM\EntityRepository;

class TestRepository extends EntityRepository {

private function calculateNumQuestions() {

    $qb = $this->getEntityManager()
                ->createQueryBuilder();

    $query = $this->getEntityManager()->createQueryBuilder()
                        ->select('COUNT(id)')
          ->from('testquestion', 'tq')
          ->where('tq.test_id = :id')
          ->setParameter('id', $this->getId())
          ->getQuery();

    $result …
Run Code Online (Sandbox Code Playgroud)

php symfony doctrine-orm

5
推荐指数
1
解决办法
3650
查看次数

标签 统计

php ×3

symfony ×3

doctrine-orm ×2

debugging ×1

mysql ×1