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?


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

Jun 1, 2011

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 View Related

UpdateAll And HABTM In CakePHP

Aug 5, 2009

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 View Related

HABTM Relationships In Cakephp Is Too Many?

Dec 31, 2010

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 View Related

Save Files With HABTM In CakePHP?

Jul 30, 2009

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 View Related

CakePHP Passing Id To Edit Form?

May 9, 2011

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 View Related

Cakephp 1.3 Paginating Habtm Related Data?

Jan 7, 2011

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 View Related

Making HABTM Relationships Unique In CakePHP?

Mar 7, 2010

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 View Related

CakePHP And HABTM Model Limit Error?

Feb 12, 2010

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 View Related

Generate List With Habtm Relationship - CakePHP

Jun 4, 2009

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 View Related

Associating CakePHP HABTM Models Without A View?

Jul 4, 2011

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 View Related

CakePHP Find() Based On HABTM Relationships?

Nov 29, 2008

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 View Related

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

Apr 8, 2011

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 View Related

Save Additional Information To A CakePHP HABTM Join Table?

Mar 29, 2011

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 View Related

Post To A Form And Populate A Select Multiple?

Oct 22, 2010

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 View Related

Populate Zend Form Select Element?

Aug 13, 2010

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 View Related

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

Sep 20, 2010

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 View Related

Populate Form - Make Select List Sticky?

Sep 22, 2010

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 View Related

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

Sep 10, 2009

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 View Related

Select List - Edit Form

May 25, 2007

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 View Related

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

Feb 9, 2011

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


View 3 Replies View Related

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

May 31, 2009

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 View Related

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

Dec 12, 2010

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 View Related

Populate Forms Edit Boxes

Jan 22, 2005

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 View Related

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

Dec 11, 2010

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 View Related

CakePHP - Add To/edit $this->passedArgs?

Apr 11, 2011

multiple parameters being passed via the url-city, cuisine, features, and what page you're on (thank you paginator)If user is on page 2 -...index/page:2 and clicks on the city "New York", they should go to...index/page:2/city:New York.Then, if they then click on Houston, they should go to ...index/page:2/city:New York_Houston.Is there any easy/built in way to do that?I've started writing my code to break apart the $this->passedArgs, and rebuild it-but this has been a pain so far, and it seems like I'm writing a ton of code for something I'm hoping Cake has a way to handle.
User is on page 2 of a restaurants list - index/page:2
User clicks "New York", the page refreshes with index/page:2/city:New York
User clicks "San Diego" - the page refreshes with index/page:2/city:New York_San


View 4 Replies View Related

CakePHP Edit Multiple Records At Once?

Sep 3, 2009

HABTM relationship between two tables:items and locations, using the table items_locations to join them.items_locations also stores a bit more information.Here's the schema

items_locations(id, location_id, item_id, quantity)

I'm trying to build a page which shows all the items in one location and lets the user, through a datagrid style interface, edit multiple fields at once:Location: Factory XYZ
| Widget | 3 |
| Sprocket | 1 |
| Doohickey | 15 |
To help with this, I have a controller called InventoryController which has:
var $uses = array('Item', 'Location'); // should I add 'ItemsLocation'?How do I build a multidimensional form to edit this data?
Edit:I'm trying to get my data to look like how Deceze described it below but I'm having problems again.

// inventory_controller.php
function edit($locationId) {
$this->data = $this->Item->ItemsLocation->find(


View 1 Replies View Related

Cakephp: Edit View Inserting Instead Of Updating?

Jan 30, 2011

If I have an edit view that is inserting a row instead of updating a row, what do I need to check?

The id of the row I'm editing is being passed in on the url:


Here are the contents of $this->data that are being passed into the edit controller function:

[Schedule] => Array
[task_id] => 1
[repeat_type_id] => 10
[priority] =>

task_id and repeat_type_id are required; priority is not.

View 1 Replies View Related

Multi-record Edit With Related Data On Cakephp?

Jan 13, 2011

simple application with CakePhp,creating a multi-record edit form using some related data.The application I'm developing is pretty straightforward, its main purpose is managing students records:updating, deleting, changing a student from one group to another, the usual suspects.The relevant tables of the database are:

group = (id, teacher, classroom, etc)
groups_students=(groupID, studentID, since, until)
students = (id, name, last_name, etc)


As you might have gathered from the tables above, the application is supposed to aid in recording assitance.Which is where I'm having some issues.What I want to do is this:Have the user select a group In de detail group, I'll have an action called "Register Assistance" Register assitance should redirect to a view in wich for every student belonging to that group, the user can see the student's assitance, edit them, and save.Something like this:In which A stands for "Absent" and P for "Present" and the user can edit everyone, and the save.

I just don't know how to go about that?How do I manage that?I've managed to create a multi-edit form for the assitance, but adding the related data is a pain, I don't know if I should query the students from the groups controllers and then pass that to the action to register assitance, or manage all the logic inside the assitance controller?

Edit: Here's the output of $this->Student->find('first');
Array (
[Alumno] => Array (
[id] => 14
[tipo] => dni


The output is in spanish.Alumno = Student, nombre= first name, apellido= last_name.

View 1 Replies View Related

Can't Edit Other Table Information On A HasOne Relationship In CakePHP?

Jun 29, 2011

I have 2 tables liked in CakePHP app, one is Users which hasOne Profile liked to.I have managed to automatically create entries in both when creating a new user, but, when I try to edit everything in my user-edit page, it will create a new profile entry, causing my user to have 2 profiles linked to.Both models are perfectly coded and linked, as I said, I'm able to create a new user and the system automatically creates the profile and links it.Here is my user_controller.php with the edit function that is not working properly:

function edit($id = null) {
if (!$id && empty($this->data)) {
$this->Session->setFlash(__('Invalid User', true));
$this->redirect(array('action' => 'index'));


View 1 Replies View Related

CakePHP Select Default Value In SELECT Input

Oct 9, 2009

Using CakePHP:

I have a many-to-one relationship, let's pretend it's many Leafs to Trees. Of course, I baked a form to add a Leaf to a Tree, and you can specify which Tree it is with a drop-down box ( tag) created by the form helper.

The only thing is, the SELECT box always defaults to Tree #1, but I would like it to default to the Tree it's being added to:

For example, calling would bring up the interface to add a new Leaf to Tree #5. The dropdown box for Leaf.tree_id would default to "Tree 5", instead of "Tree 1" that it currently defaults to.

What do I need to put in my Leaf controller and Leaf view/add.ctp to do this?

View 3 Replies View Related

CakePHP - Edit Loses URL Variable When Trying Validation Fails On Save?

Apr 7, 2011

User starts here:
User types something in that is against validation rules and clicks Submit.User is taken here:/admin/cuisines/edit/16
User thinks it IS valid and clicks submit again-they're then taken here:/admin/cuisines/edit/
User corrects their mistake, clicks submit, and instead of an edit, it saves this as a new item in the table, since there's no id.
My code:
function admin_edit($id = null) { // EDIT ***********
if (!$id && empty($this->data)) {
$this->Session->setFlash(__('Invalid cuisine', true));
$this->redirect(array('action' => 'index'));


Any thoughts on what I'm doing wrong?I thought I did it just like the tutorial, but-I would have to guess Cake is smart enough to not let this happen-ie-my assumption is I'm doing something wrong.

View 1 Replies View Related

Populate Select Box With Database Row?

Sep 27, 2010

I'm trying to populate a select box with rows from a table, but it's not showing up properly. I've looked at the source of the page, and it looks like the option html is not being outputted.

<h1>Upload Picture</h1>
<form action="gallery.php" method="post">
<label for="photo">Select file: </label><input type="file" name="photo" />
<select name="photoGallery">


View 3 Replies View Related

Populate The Select Box With The Values?

May 25, 2011

A table in my MySQL database called studentname.made an array in Javascript as var array1=["john","sam","peter","kelvin"]; populated a select box with these array values.If the table studentname contains names like "sam" and "peter", now want to populate the select box with the values "john" and "kelvin" only, because "sam" and "peter" are already present in the database table.

View 1 Replies View Related

Using PHP And MySQL To Populate (select) Menus

Oct 30, 2000

I am a newbie to php *gasp* and I am trying to make a <select> drop down menu, but I want the menus populated by information from a field in my database. There are a bunch of entries in the field MUD, that are identical......I would like the menu NOT to repeat identical names. EX... Each name only once, even though it is in the database many times.

View 3 Replies View Related

Populate Select Box With Data From Mysqltable?

May 19, 2011

How to populate select box with data from mysql table, and then move that value from one page to another.I did a small coding, and i was able to get all the values from table, but i cant move those value to other page.

$query1="SELECT * FROM seat_no WHERE seatno NOT IN(SELECT seatno FROM check_in_desk)";
$result1 = mysql_query($query1);
<select name="txt_seatno">[code]....

View 2 Replies View Related

Auto Populate Select Boxes?

Feb 11, 2010

I'm having difficuties in getting 2 select boxes to automatically update, when a specific option is chosen from the 1st select box, the 2nd box should update accordingly.I should also point out that i am populating my select boxes from a database.My database structure is as follows:


View 4 Replies View Related

Populate An Associative Select From A Database?

Jul 8, 2011

Here's my problem: I have two select fields in my web application.

The second one depends on the choice made on the first one. The first one is populated from my database with the classical:

//connection to database in an include file
$query = "SELECT DISTINCT platform
FROM Appversions";
$res = mysql_query($query);


I understand that I need to use JavaScript with an onChange function call to do that, but I can't figure out what that function should look like or how it will have access to my query result.

View 1 Replies View Related

Populate Select List From JSON?

Jun 5, 2011

I am attempting to build a select box using JSON object retrieved from a PHP script. The PHP script (versions.php) is running a query against a database table; code is as follows:

$posts = array();
if(mssql_num_rows($result)) {
while($post = mssql_fetch_assoc($result)) {
$posts[] = $post;


Looking at firebug, I see lists of objects, but not the values in the array. I guess this is related to the code in the javascript file above - just not sure how to access the values from the json object.

View 1 Replies View Related

Populate Related Data In Top Of Select Box?

Sep 28, 2010

I want to put one select box to select users.... Now in my database table user_table contains more than 1000 records... But i don't want to waste the time to select appropriate user from select box.. Thats why i put one text box on the top of the select box, if i type any character in it,the select box will shows the related records on top of the select box of that character... If i type two char's, it is again will show the much related on the top of the select box...

View 6 Replies View Related

Populate Select Box Using Database Values?

Mar 11, 2009

I'm trying to populate a select list from values in a table. I cannot get it to work, so I would appreciate any assistance in getting the values to be displayed. Here is my code:

<form name="addStudent" method="post">
Student: <select><?php
$sql="SELECT name FROM student";
$result = mysql_query($sql);

View 4 Replies View Related

Populate Table From Select Query

Mar 12, 2007

What is the best way of putting data into a html table based on a select query with a specified number of columns?

$query_string = ("SELECT col1, col2 FROM tatble");

View 1 Replies View Related

Html - Auto Populate A Select Box Using An Array?

Mar 9, 2011

If i had a select box


and I had an array of values from 1-12

using php, how would I auto populate that select box using that array?

View 2 Replies View Related

Populate HTML Select List Using Javascript?

Jun 25, 2011

I'm pretty new to HTML and javascript. I know this code already exist on internet but I can't have it working for me. I'm stuck on this issue for 2-3 days.

I want to populate the optCategory select list based on the selected entry of optPostAppliedFor. For that I called a function change_categoriees(key) when I click the optPostAppliedFor list. The code is here as follows


View 4 Replies View Related