cakePHP hasOne 关系不自动完成下拉字段

发布于 2024-11-16 21:13:04 字数 1773 浏览 3 评论 0原文

我正在尝试在两个模型之间实现 hasone 关系,但我无法使用第二个模型(属于第一个模型的那个)中的可能选项自动完成“添加”表单。这是我的代码:

- 模型 1: item.php

<?php
class Item extends AppModel{ 

    var $name = 'Item';
    var $primaryKey = 'id';
    var $hasOne = 'CoverImage';
    }
?>

- 模型 2: cover_image.php

<?php
class CoverImage extends AppModel{ 

    var $name = 'CoverImage';
    var $primaryKey = 'id';
    var $belongsTo = array(
                 'Item' => array(
                    'className' => 'Item',
                    'foreignKey' => 'item_id'
                        )); 
}
?>

- 添加模型 2 的视图: add.ctp

<?php echo $this->Form->create('CoverImage',array('url' => array('controller' => 'admins', 'action' => 'add')));?>
    <fieldset>
        <legend><?php __('Info'); ?></legend>
    <?php
        echo $this->Form->input('item_id');
        echo $this->Form->input('description');
    ?>
    </fieldset>
    <?php echo $this->Form->end(__('Create', true));?>

对于我在 Cake 文档中看到的内容,通过这种关系,在 add 视图中,我应该在 item_id 字段中看到一个下拉列表,以便能够选择执行此操作的项目封面图片属于到,但下拉列表是空的(是的,我的项目表中已经有一些项目)。

也许我错过了什么或者我做错了什么,但我无法弄清楚。预先非常感谢您提供任何线索!

编辑

我刚刚意识到,如果我这样做:

echo $this->Form->input('item_id', array('type'=>'text'));

而不是这样:

echo $this->Form->input('item_id');

我可以添加/编辑 *item_id* 字段,我可以在文本框中看到它的值。但是,如果我离开另一个,我只会看到一个空的保管箱,当我尝试添加/编辑CoverImage时,它不起作用,它只显示一个空的白色页面,甚至没有错误...

也许这是导致某些事情的原因...

I'm trying to implement a hasone relationship between 2 models, but I can't have the 'add' form autocomplete with the possible options in the second model (the one that belongsTo the first one). This is my code:

- model 1: item.php

<?php
class Item extends AppModel{ 

    var $name = 'Item';
    var $primaryKey = 'id';
    var $hasOne = 'CoverImage';
    }
?>

- model 2: cover_image.php

<?php
class CoverImage extends AppModel{ 

    var $name = 'CoverImage';
    var $primaryKey = 'id';
    var $belongsTo = array(
                 'Item' => array(
                    'className' => 'Item',
                    'foreignKey' => 'item_id'
                        )); 
}
?>

- add view of model 2: add.ctp

<?php echo $this->Form->create('CoverImage',array('url' => array('controller' => 'admins', 'action' => 'add')));?>
    <fieldset>
        <legend><?php __('Info'); ?></legend>
    <?php
        echo $this->Form->input('item_id');
        echo $this->Form->input('description');
    ?>
    </fieldset>
    <?php echo $this->Form->end(__('Create', true));?>

For what I see in Cake's documentation, with this relationship, in the add view I should see a dropdown list in the item_id field to be able to select to which item does this CoverImage belongs to, but the dropdown is empty (and yes, I have some items in the items table already).

Maybe I'm missing something or I've done something wrong, but I can't figure it out. Thanks so much in advance for any clues!

EDIT

One think I've just realized is that if I do this:

echo $this->Form->input('item_id', array('type'=>'text'));

instead of this:

echo $this->Form->input('item_id');

I can add/edit the *item_id* field, I can see its value in the text box. However, if I leave the other one, I just see an empty dropbox and when I try to add/edit a CoverImage, it doesn't work, it just shows an empty white page, not even with errors...

Maybe this is a lead to something...

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

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

发布评论

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

评论(2

说不完的你爱 2024-11-23 21:13:04

为了使其工作,您必须在控制器中创建可能选项的列表。这不会自动发生。

public function add() {
    $items = $this->CoverImage->Item->find('list');
    $this->set(compact('items'));
}

FormHelper 仅自动推断字段 item_id 应该由变量 $items 中的选项填充(复数,无 _id)。

请注意,已经有一个 CoverImage 的项目不应成为该列表的一部分。 find('list', array('conditions' => array('CoverItem.id' => null))) 可能*会处理这个问题,但您还需要在保存之前重新检查,或者您需要重新考虑您的关联。

* 我完全不确定这是否适用于 'list' 搜索。

In order for that to work you have to create a list of possible options in the controller. That does not happen automatically.

public function add() {
    $items = $this->CoverImage->Item->find('list');
    $this->set(compact('items'));
}

The FormHelper only automatically infers that the field item_id should be populated by the options in the variable $items (plural, no _id).

Do be careful that Items that already haveOne CoverImage should not be part of that list. find('list', array('conditions' => array('CoverItem.id' => null))) will probably* take care of that, but you'll need to recheck just before saving as well, or you need to rethink your associations.

* Not sure off the top of my head whether that'll work for 'list' searches.

痞味浪人 2024-11-23 21:13:04

非常好的问题。您遇到了 Cake 关联的一个不诚实的功能:

考虑到您将关系定义为 hasOne?通过猜测痕迹,Cake 甚至可能正确地推断出您对列表功能的偏好。你得到了你的自动魔法列表...

... of One.

$hasOne 是非常独特的。它“用完了”那些“有”关系(它使关系成为事实上的单例 - 因此用户只有 1 个配置文件 <-> 配置文件只有 1 个用户)。考虑一下 - 数据库可以有多种配置,但 Dbo 一次只能有一个连接,而连接也只能有一个 Dbo。因此-> hasOne 用于将两个模型结合起来,直到 die() 分开。

-- 所以它的使用率几乎没有 hasMany 和 BelongsTo 那么多。

出于您的目的,您可能想要更改为不同的关联。

添加额外的 $this->Item->find 并不能真正解决问题(我不会推荐它,除非你已经完成了两个模型/控制器的工作,或者你积极地希望事情开始变得奇怪。)

此外,改变你调用表单助手方法的方式 - 如果你从查找中返回“列表”类型的获取,Cake 会自动生成一个选项列表 它。实际发生的情况是,您在视图功能的微小边缘上偷偷摸摸地使用模型。这就是为什么指定输入类型来“打破魔法”往往不被鼓励(如果你愿意的话,你完全可以。只要了解实际发生的事情,或者:看,奇怪,快。)

但是你可能想重新思考你的方式已经关联了您的模型 - 难道说每个项目都属于一个 CoverImage (与每个 CoverImage 属于一个项目相同)不是也正确吗 - 因为您有一个明确允许 CoverImage 选择一个项目(任何项目)的表单显示为?您可能会得到更好的结果。

HTH。 :)

EXCELLENT QUESTION. You've run afoul of a disingenuous feature of Cake's associations:

Considering you defined the relationship as hasOne? Guessing at the trace but Cake probably even correctly inferred your preference for list functionality. You got your automagic list...

...of One.

$hasOne is pretty exclusive like that. It "uses up" those "has" relationships (it's makes the relationship a de facto Singleton - so Users only have 1 Profile <-> Profile only has 1 User). Consider - Database can have many configurations, but Dbo will only ever have one Connection at a time and Connection will only have one Dbo. Thus -> hasOne is for marrying two Models til die() do they part.

-- So it doesn't get used nearly as much as hasMany and belongsTo.

For your purpose, you probably want to change to a different association.

Adding an additional $this->Item->find doesn't really fix what's wrong (and I wouldn't recommend it, unless you're mostly done with both models/controllers, or you actively want things to start getting weird fast.)

Also, changing how you call the Form Helper methods - if you return a 'list' type fetch from a find, Cake will automatically produce an option list out of it. What's actually happening is, you're sneaking around your Model on a very thin margin of View functionality. That's why specifying the input type to "break the magic" tends to be discouraged (which, you totally can if you want. Just understand what's actually happening, or: see, weird, fast.)

But you might want to rethink how you've associated your models - wouldn't it also be correct to say, each Item belongsTo a CoverImage (same as each CoverImage belongs to an Item) -- because you have a form expressly permitting a CoverImage to select an Item, any Item, to be displayed with? You'll probably get better results.

HTH. :)

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