Pomm Twig 和关系
Pomm Twig and relation
对于我正在使用的项目 pommbundle,它非常适合使用现有数据库生成实体。
在我的控制器中:
$catalogues = $this->get('pomm')['my_db1']
->getModel('\AppBundle\Entity\MyDb1\PublicSchema\CatalogueModel')
->findAll();
return this->render(
'SiteBundle:Default:homePage.html.twig',
array('catalogues'=>$catalogues));
但是我如何访问视图中的变量 (Twig
)
{% for catalogue in catalogues %}
{{dump(catalogue)}} --> value inside
{% endfor %}
结果转储
Catalogue {#1132 ▼
#container: array:13 [▼
"ID" => 8
"Code" => "MATIÈRE PREMIÈRE"
"Actif" => true
"DateAjout" => DateTime {#1212 ▶}
"Index" => 0
"PriseCommande" => false
"Description" => ""
"Couleur" => "Green"
"CouleurText" => "#000000"
"Tarif" => null
"WebActif" => false
"WebTitre" => null
"WebDescription" => null ]
-status: 1 }
catalogue.ID
(无效)catalogue.container.ID
(无效)
使用 catalogue.get('ID') 可行,但这是最好的方法?
其他问题
如果我的实体有关系,例如WebActif -> relation with another table
,
如何访问 Webactif 因为转储 returns 只有一个 ID.Do 我必须创建自己的方法?
是否可以展示一个基本示例?
Model::findAll
方法 returns 数据库结果的迭代器。遍历此迭代器时,它 returns 个实体填充了转换后的值。
注意:您最好不要在列名中使用大写字母,因为这会导致混淆并且不会起作用 正确地与 Pomm 柔性实体。 (同样适用于 table 个名字)。
<dl>
{% if catalogues.isEmpty() %}
<dt>No results found.</dt>
{% else %}
<dt>There are {{ catalogues.count() }} results:</dt>
{% for catalogue in catalogues %}
<dd>{{ catalogue.code }} (added the {{ catalogue.date_ajout.format('d-m-Y') }}){% if catalogue.actif %} OK {% endif %}</dd>
{% endfor %}
{% endif %}
</dl>
编辑: 因为你的评论说你的数据库包含大写的列名,这里是关于灵活实体如何工作的额外解释。
(关于灵活实体的官方文档是here)
当一个灵活的实体被迭代器值混合时,它们会被转换,然后用它们的名字推送到实体中。这就是您可以使用通用访问器 $entity->get('MyColumn')
的原因,因为键被保留了。
但是灵活的实体是奇怪的野兽,因为它们可以根据决定发送给它们的数据的 SELECT
而改变。当创建这样的实体时,getters 和 setters 实际上是使用 PHP 的 __get
和 __set
和 __call
函数创建的。
这看起来很奇怪,但看看这个例子:
<?php
$entity = new MyEntity(['first_name' = 'John', 'last_name' => 'Doe']);
$entity['first_name']; // calls $entity->getFirstName() which defaults to $this->get('first_name');
然后可以覆盖默认访问器:
<?php
class MyEntity extends FlexibleEntity
{
/*
* Triggered by $entity['first_name']
* or $entity->first_name
*/
public function getFirstName(): string
{
return uc_words($this->get('first_name'));
}
public function getLastName(): string
{
return strtoupper($this->get('last_name'));
}
public function getName(): string
{
return sprintf("%s %s", $this->getFirstName(), $this->getLastName());
}
}
然后,在 twig 中可以简单地执行 {{ entity.name }}
来触发 getName
函数。
如您所见,列名是驼峰式的以创建虚拟访问器,只有当原始列名是小写时才能反转此操作。
对于我正在使用的项目 pommbundle,它非常适合使用现有数据库生成实体。
在我的控制器中:
$catalogues = $this->get('pomm')['my_db1']
->getModel('\AppBundle\Entity\MyDb1\PublicSchema\CatalogueModel')
->findAll();
return this->render(
'SiteBundle:Default:homePage.html.twig',
array('catalogues'=>$catalogues));
但是我如何访问视图中的变量 (Twig
)
{% for catalogue in catalogues %}
{{dump(catalogue)}} --> value inside
{% endfor %}
结果转储
Catalogue {#1132 ▼
#container: array:13 [▼
"ID" => 8
"Code" => "MATIÈRE PREMIÈRE"
"Actif" => true
"DateAjout" => DateTime {#1212 ▶}
"Index" => 0
"PriseCommande" => false
"Description" => ""
"Couleur" => "Green"
"CouleurText" => "#000000"
"Tarif" => null
"WebActif" => false
"WebTitre" => null
"WebDescription" => null ]
-status: 1 }
catalogue.ID
(无效)catalogue.container.ID
(无效)
使用 catalogue.get('ID') 可行,但这是最好的方法?
其他问题
如果我的实体有关系,例如WebActif -> relation with another table
,
如何访问 Webactif 因为转储 returns 只有一个 ID.Do 我必须创建自己的方法?
是否可以展示一个基本示例?
Model::findAll
方法 returns 数据库结果的迭代器。遍历此迭代器时,它 returns 个实体填充了转换后的值。
注意:您最好不要在列名中使用大写字母,因为这会导致混淆并且不会起作用 正确地与 Pomm 柔性实体。 (同样适用于 table 个名字)。
<dl>
{% if catalogues.isEmpty() %}
<dt>No results found.</dt>
{% else %}
<dt>There are {{ catalogues.count() }} results:</dt>
{% for catalogue in catalogues %}
<dd>{{ catalogue.code }} (added the {{ catalogue.date_ajout.format('d-m-Y') }}){% if catalogue.actif %} OK {% endif %}</dd>
{% endfor %}
{% endif %}
</dl>
编辑: 因为你的评论说你的数据库包含大写的列名,这里是关于灵活实体如何工作的额外解释。
(关于灵活实体的官方文档是here)
当一个灵活的实体被迭代器值混合时,它们会被转换,然后用它们的名字推送到实体中。这就是您可以使用通用访问器 $entity->get('MyColumn')
的原因,因为键被保留了。
但是灵活的实体是奇怪的野兽,因为它们可以根据决定发送给它们的数据的 SELECT
而改变。当创建这样的实体时,getters 和 setters 实际上是使用 PHP 的 __get
和 __set
和 __call
函数创建的。
这看起来很奇怪,但看看这个例子:
<?php
$entity = new MyEntity(['first_name' = 'John', 'last_name' => 'Doe']);
$entity['first_name']; // calls $entity->getFirstName() which defaults to $this->get('first_name');
然后可以覆盖默认访问器:
<?php
class MyEntity extends FlexibleEntity
{
/*
* Triggered by $entity['first_name']
* or $entity->first_name
*/
public function getFirstName(): string
{
return uc_words($this->get('first_name'));
}
public function getLastName(): string
{
return strtoupper($this->get('last_name'));
}
public function getName(): string
{
return sprintf("%s %s", $this->getFirstName(), $this->getLastName());
}
}
然后,在 twig 中可以简单地执行 {{ entity.name }}
来触发 getName
函数。
如您所见,列名是驼峰式的以创建虚拟访问器,只有当原始列名是小写时才能反转此操作。