Cakephp Populate HABTM Select Box In EDIT Form?

Apr 19, 2011

I am using cakephp's form for an HABTM relationship between User and Event. There is a join table events_users. The form works well for the Add form, but not for the edit form. The code is:


$this->data = $this->Event->findById($id);

Form field:


What I get is a blank, input box. Unlike the select box in the Add form. Any ideas what I'm doing wrong or how to populate a select box with the right Users selected for the input on the edit form?

View 1 Replies


Drupal Get Parameters From Url In Edit Form And Populate The Form?

i have an employee table with fields like id, name, age and salary.

Iam showing the list of employee names in my custom module and when i click on an employee name, i have to show the edit form of that employee.

The employee names are given a link, as like:

<a href='".$base_url."/my_module/edit/".$employee->id."'>

and the corresponding menu path is configured as:

$items['my_module/edit/%'] = array(
'title' => t('My form'),
'description' => t('Edit employee'),
'page callback' => 'my_module_edit',
'access arguments' => array('access content'),
'access callback' => TRUE,
'type' => MENU_CALLBACK,

And when i click on the 5th employee name, the url would be: ..../my_module/edit/5

So now my questions are:

How can i get the id (5) of the employee from the url in my my_module_edit function ? How can i show the edit form of the employee, populating the fields (by querying with id) ?

View 3 Replies

UpdateAll And HABTM In CakePHP

I have two models and a join table, User, Post and posts_users. The posts_users table contains a few extra fields, one of which is is_active. I want to set all of a users posts to active providing the user is active themselves. I have the following

$this->Post->PostsUser->updateAll(array('PostsUser.is_active' => 1), array('PostsUser.user_id' => $data['User']['id'], 'User.is_active' => 1));

Which causes a MySQL error

Warning (512): SQL Error: 1054: Unknown column 'User.is_active' in 'where clause' ... Query: UPDATE posts_sites AS PostsUser SET PostsUser.is_active = 1 WHERE PostsUser.user_id = 1 AND User.is_active = 1

As you can see the User table isn't getting joined in the query.

View 1 Replies

HABTM Relationships In Cakephp Is Too Many?

I'm struggling with deciding how many HABTM relationships I really need in my cakephp app. I guess I don't quite get what "has" truly means. I don't want to have more HABTM tables than necessary. Here's what I have in my database:


user HABTM properties, user HABTM leases, user HABTM users, user HABTM repairs, property hasMany repairs, property hasMany leases, lease hasMany repairs.

Also, I have two aliases for my users model (manager and tenant). This is one of the reasons I ended up with so many HABTM relationships, but I'm not sure it's necessary.

Do I have too many user HABTM relationships? Would it be better to just have cake recurse through my models?

View 1 Replies

Save Files With HABTM In CakePHP?

I am creating multiple associations in one go and there are a few problems when it comes to saving.I have the following code:

foreach($userData as $user) {

View 2 Replies

CakePHP Passing Id To Edit Form?

many different ways to pass an ID to a form when editing a database entry.So for example for a edit user profile form I have the following code:

function edit($id = null)
$this->layout = 'page';
// this line isn't needed?
//$this->User->id = $id;


Now the function expects an id passing in the url e.g./users/edit/2 But let's say I wanted it to be something more user friendly like /profile/edit(rewrote by routing) I would no longer be passing in the ID as part of the url.As you can see in my code I have a line I have commented out because it isn't needed?Also in the form I ALSO Need <?php echo $this->Form->input('id', array('type' => 'hidden')); ?>why?Basically this is more of what are the options available to me to build various types of edit forms and passing data to the form.And what is the need for the hidden field in the form if the data is being passed either via the URL or some other way.

I've also noticed on some sites that they have things like Form Keys and the username stored in meta tags in the page header?
public function beforeFilter()
$this->set('authUser', $this->Auth->user());
$user = $this->Auth->user();


View 1 Replies

Cakephp 1.3 Paginating Habtm Related Data?

I have searched high and low and tried multiple different solutions to this and I cannot seem to get it to work. Basically I have products and categories. Categories has and belongs to many products, and products HABTM categories. They are obviously related in a join table products_categories with id,product_id,category_id fields.

After a simple full bake- models views and controllers are baked - some categories and products are added and their relationships are set I can go to a specific category view page and the pre baked view will show products that are related to this category.

However, there is quite a few products (and will be more) and I am struggling to work out how to paginate all of this related it is unfeasible to just simply list all of the products in a category (there could be 100s or more) Can anybody help? It seems there are quite a few unanswered topics on this and various different solutions, none of which I can get to work!

View 1 Replies

Making HABTM Relationships Unique In CakePHP?

I have two models, called Book and Tag, which are in a HABTM relationship. I want a couple (book, tag) to be saved only once. In my models I have

var $hasAndBelongsToMany = array(
'Tag' => array(
'className' => 'Tag',


View 4 Replies

CakePHP And HABTM Model Limit Error?

I've got a series of Post models that hasAndBelongsToMany Media models. In certain function calls inside of the Post model, I don't need to retrieve the entire list of Media models. However, when I use the following code:

$this->unbindModel( array('hasAndBelongsToMany' => array('Media')) );
// Rebind to get only the fields we need:


View 5 Replies

Generate List With Habtm Relationship - CakePHP

I want to generate a list (dropdown) with habtm relationship, for example i have a table called "users" another called "vehicles" and "users_vehicles".

What i want is to get vehicles assigned to "x" user and put it in a dropdown, how i can achieve this?

class User extends AppModel {
var $hasAndBelongsToMany = array('Vehicle');

View 2 Replies

Associating CakePHP HABTM Models Without A View?

I'm writing an import from CSV and have been able to successfully manually set data fields on a model in code by using the $this->Book->set() function and passing it a hash containing all of the fieldName => value pairings.

How do I create habtm associations in code? All the examples I've seen in the documentation are based on the $this->data returned from a form in the view. Because my data is coming from a csv file and not a view I can't use this!

So in the following example:

// Book habtm Tags
// Tag habtm Books


How would I associate the $arrayOfTagIds with the Book?

View 1 Replies

CakePHP Find() Based On HABTM Relationships?

I'm working on a CakePHP 1.2 application. I have a model "User" defined with a few HABTM relationships with other tables through a join table. I'm now tasked with finding User information based on the data stored in one of these HABTM tables. Unfortunately, when the query executes, my condition is rejected with an error about a missing table. Upon inspection it seems that CakePHP is not including any of the HABTM tables in the select statement.

My User HABTM relationship is as follows:

var $hasAndBelongsToMany = array(
'Course' => array(
'className' => 'Course',
'joinTable' => 'course_members',
'foreignKey' => 'user_id',
'associationForeignKey' => 'course_id',


View 5 Replies

CakePHP - HABTM - Sending Data From Multiple Tables To A View?

3 tables:

2 join tables:

What is the best way to get data for a single restaurant from my controller to the view which includes not only the restaurant data, but also the cuisines and features that it has.

I have it working with this:

$restaurant = $this->Restaurant->find('all', array('conditions' =>
array('' => $id)));
$this->set('restaurant', $restaurant);

And I'm calling it in the view like this:

echo "<span class='label'>Name:</span> " . $restaurant[0]['Restaurant']['name'] . "<br />";
echo "<span class='label'>Cuisine:</span> ";
$cuisineList = "";
foreach($restaurant[0]['Cuisine'] as $cuisine) {
$cuisineList .= $cuisine['name'] . ", ";
echo substr($cuisineList,0,-2) . "<br />";

(and repeat for Features)But this seems overkill, since in all the Cake tutorials I see, the view method in the controller just has:

$this->set('restaurant', $this->Restaurant->read(null, $id));

Then they call it like this:

echo $restaurant['Restaurant']['name'];
Again -it IS working-I just want to know if there's a better way-the way I'm currently doing it seems sloppy compared to all the other slick things Cake does for me!

View 1 Replies

Save Additional Information To A CakePHP HABTM Join Table?

I'm unable to save a record to a HABTM join table manually. I have a table, users_products and would like to save all additional information about the order (size, quantity, color, etc...) to this join table, instead of creating a separate one. User $hasAndBelongsTo Product My users controller code to do so:

Configure::write('debug', 0);
//adds product to cart
$this->layout = ""; //async
$user = $this->User->find('first',array('conditions'=>array(''=>$this->Auth->user("id"))));
$data['User'] = $user['User'];
$addition = array('user_id' => $this->Auth->user("id"),
'product_id' => $id,

Unfortunately, this adds a new record to the database and clears out the entire join table while doing so.

View 2 Replies

Post To A Form And Populate A Select Multiple?

I'm doing something a bit strange here, I'm querying data out of my local database and posintg it to Salesforce form using cURL. The data posts correctly to Salesforce. However, the select multiple is not getting the correct values selected. See $sd["location"] below in my code:

//init curl
$ch = curl_init();
//setup the params
$url = '[URL]...';
$oid = "HIDDEN";
//setup the superdelegate array
$sd = array();
$sd["oid"] = $oid;............

The select multiple is already setup in Salesforce with a 30 different locations. I'm trying to pass the cities that should be selected (Chicago, New York, California).

View 3 Replies

Populate Zend Form Select Element?

I have been searching for it from last couple of hours and have read every matching result google can give me but still can't get it to work.

I am creating a zend form select element through:

this->addElement('select','my_select', array(
'label' => 'Currency', 'value' => 'blue',
'multiOptions' => array( 'red' => 'Rouge', 'blue' => 'Bleu', 'white' => 'Blanc', ), ) );

now I want to populate it through


from the controller, I have tried giving double dimensional array like

$vals = array("my_select" => array("US Dollar", "Pound Sterling"))

and then giving it in:


but this didnt worked and I am not sure if this will work, at the moment I am building my array like in array( 'red' => 'Rouge', 'blue' => 'Bleu', 'white' => 'Blanc') the same class as zend form and then passsing it to the addElement multiOptions dynamically, here: [URL] that works but I would like to have it through populate

View 1 Replies

Limit The Maximum Number Of HABTM And HasMany Records SAVED Per Parent Model In CakePHP?

I'm rebuilding one of my sites using the CakePHP Framework (awesome!), and I need to limit the number of hasMany and HABTM records that a Model can have. Let's say for "Profile hasMany Image," I want to limit a profile to only 10 images.

I know I can easily accomplish this using the Find method with 'count,' but I'm wondering if there's a property that can be set somewhere that I overlooked. I've looked at the CakePHP documentation and Googled a bit, haven't seen anything, but I'm always running across little CakePHP secrets now and then.

View 2 Replies

Populate Form - Make Select List Sticky?

I am using the code below to populate a select list from fields in a db and it works great, the problem is how do I make it sticky? if the user types in the wrong info the page reloads and it does not remember what was selected from the list. I have tried a few different if statements but i couldn't get it working. The list populates with 15 customer names, what would be the best way to tackle this?


View 2 Replies

Populate Form Select Options - Table Breaks When Add The <select> And It's Options?

I've got a table with 7 columns and I'm trying to populate one of the cells with an html select an it's options based on the results of the data row.The problem is as soon as I add the <select> and <options> it doesn't render the last three cells on the line where the condition is true.This link is not using the <select><option> stuff and table looks fine.

Source of that file.

When the Category is == NONE I'm trying to get a select built as demonstrated here, but the last few cells on those rows do not get populated.

Source of that file.

give the query a few seconds to run, it's kind of slow. why this table breaks as soon as I add the <select> and it's options.

View 1 Replies

Select List - Edit Form

For a couple of days I have been trying to make an edit form that remembers the initial values a user entered. The edit.php page has a few textboxes and a few dropdown/select boxes. The textbox part works fine and comes from a database. Code:

View 7 Replies

CakePHP Multiple Entry Fields With SaveAll() HABTM Multiple Records Insert Not Saving

Everything below IS working with the exception of my HABTM Zip data. Here is my code:


View 3 Replies

Form Select Options - Populate The Options For The User To Pick With A List From A Database?

I have a Form control with a Select element. I want to populate the options for the user to pick with a list from a database. How do I get this into the Select Options? Is there a relatively simple way with HTML or is this a question for PHP?

View 3 Replies

Populate A "Select" Box Drop Down Menu In A Form With Database Fields?

I am currently creating a form and I want to populate a drop down selection menu with data from two fields in a form. For example, I want it to pull the first and last name fields from a database to populate names in a drop down menu in a form. I want the form to submit to the email address of the person selected in the drop down. Is this possible to do? The email is already a field in the record of the person in the database.

how I should go about setting up the "Select" box drop down? I am not sure how to code it to do what I am wanting.

View 10 Replies

Html - Populate A Second Select-tag With Options Based On The Option Selected In The First Select-tag?

I will draw the options for the second select-tag from a database.

View 4 Replies

Populate Forms Edit Boxes

I have two simple text boxes in my <form> declaration On the same page I gave PHP code on top which is called from within tha same html portion of the page at the bottom.

Is there a way to dynamically populate the existing text box in the html part using code from the PHP part?

View 1 Replies

Auto Populate Dropdown Without Having 'id' Field In CakePHP?

I have two tables:

countries with fields [objid(PK), name]
cities with fields [objid(PK), name, country_id(FK)]

Using cakePHP, what would be the correct syntax of using $belongsTo in city model to auto populate the countries drop down?

View 1 Replies

Copyrights 2005-15, All rights reserved