在 schema.yml 中设置 Doctrine_Collection 键映射属性

发布于 2024-11-30 11:31:54 字数 699 浏览 7 评论 0原文

在 Doctrine 1.2 中,可以设置 键映射 用于表,其中由该表创建的 Doctrine_Collection 对象将从集合中每条记录的特定列填充键。

上面链接的文档中的示例:

您可能想要映射名称列:

//test.php

// ...
$userTable = Doctrine_Core::getTable('用户');

$userTable->setAttribute(Doctrine_Core::ATTR_COLL_KEY, '用户名');

现在用户集合将使用名称列的值作为元素索引:

//test.php

// ...
$users = $userTable->findAll();

foreach($users as $username => $user) {
    回显$用户名。 '-'。 $user->created_at 。 “\n”;
}

有没有办法在 schema.yml 文件中进行设置?

In Doctrine 1.2, it is possible to set up Key Mapping for a table where Doctrine_Collection objects created by that table will populate keys from a particular column in each record in the collection.

An example from the documentation linked above:

You may want to map the name column:

// test.php

// ...
$userTable = Doctrine_Core::getTable('User');

$userTable->setAttribute(Doctrine_Core::ATTR_COLL_KEY, 'username');

Now user collections will use the values of name column as element indexes:

// test.php

// ...
$users = $userTable->findAll();

foreach($users as $username => $user) {
    echo $username . ' - ' . $user->created_at . "\n";
}

Is there a way to set this up in a schema.yml file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

很糊涂小朋友 2024-12-07 11:31:54

在探索类似的问题时,我遇到了 这个例子

<前><代码>---
用户:
列:
...
属性:
出口:全部
验证:正确

对 coll_key 属性应用相同的原则会产生以下结果:

User:
  columns:
    ...
  attributes:
    coll_key: username

我们可以在构建后验证该属性是否被接受:

$this->setAttribute(Doctrine_Core::ATTR_COLL_KEY, 'username');

不过,有一个警告。您必须显式创建要使用的列,否则 Doctrine 将在构建过程中抛出错误:

User:
  actAs:
    Sluggable:  ~
  columns:
    ...
  attributes:
    coll_key:  slug
$ symfony doctrine:build --all --no-confirmation
>> doctrine  Dropping "doctrine" database
>> doctrine  Creating "dev" environment "doctrine" database
>> doctrine  generating model classes
>> file+     /tmp/doctrine_schema_60681.yml
   ...
>> doctrine  generating form classes

  Couldn't set collection key attribute. No such field 'slug'.

要使上述工作正常,您需要显式指定 slug 列,甚至尽管 Sluggable 模板通常会自动为您创建它:

User:
  actAs:
    Sluggable:  ~
  columns:
    ...
    slug:
      type:    string(50)
      unique:  true
  attributes:
    coll_key:  slug

While exploring a similar issue, I came across this example:

---
User:
  columns:
    ...
  attributes:
    export: all
    validate: true

Applying the same principle with the coll_key attribute yields this:

User:
  columns:
    ...
  attributes:
    coll_key: username

We can verify after doing a build that the attribute was accepted:

$this->setAttribute(Doctrine_Core::ATTR_COLL_KEY, 'username');

There is one caveat, though. You have to explicitly create the column that you want to use, or else Doctrine will throw an error during the build process:

User:
  actAs:
    Sluggable:  ~
  columns:
    ...
  attributes:
    coll_key:  slug
$ symfony doctrine:build --all --no-confirmation
>> doctrine  Dropping "doctrine" database
>> doctrine  Creating "dev" environment "doctrine" database
>> doctrine  generating model classes
>> file+     /tmp/doctrine_schema_60681.yml
   ...
>> doctrine  generating form classes

  Couldn't set collection key attribute. No such field 'slug'.

To get the above to work, you would need to explicitly specify the slug column, even though the Sluggable template normally creates it for you automatically:

User:
  actAs:
    Sluggable:  ~
  columns:
    ...
    slug:
      type:    string(50)
      unique:  true
  attributes:
    coll_key:  slug
海拔太高太耀眼 2024-12-07 11:31:54

如果可能的话,也没有很好的记录。
您可以在表定义中指定表的选项, 像这样
知道

const ATTR_COLL_KEY             = 108;

我会尝试:

User:
  options:
    attr_coll_key: username

然后

User:
  options:
    attrCollKey: username

甚至

User:
  options:
    108: username

我无法准确找到代码中处理选项的位置,但您可以使用 xdebug 逐步调试来完成此操作。

祝你好运,并告诉用户这些尝试之一是否有效。

If it is possible, it is not well documented.
You can specify options for a table in the table definition, like this
Knowing that

const ATTR_COLL_KEY             = 108;

I would try :

User:
  options:
    attr_coll_key: username

then

User:
  options:
    attrCollKey: username

or even

User:
  options:
    108: username

I couldn't find precisely where in the code the options are handled, but you could do this with xdebug step-by-step debugging.

Good luck, and tell use if one of these tries works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文