Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6060573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:50:28+00:00 2026-05-23T08:50:28+00:00

I’m trying to implement a hasone relationship between 2 models, but I can’t have

  • 0

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:50:29+00:00Added an answer on May 23, 2026 at 8:50 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.