Ok. I have changed the tables and these are my files:

<?php
/**
 * @property Merit $Merit
 */

class Merit extends AppModel {

    var $name = 'Merit';
}
?>

-----------------------------

<?php
/**
 * @property Category $Category
 */

class Category extends AppModel {

    var $name = 'Category';
}
?>

-----------------------------

<?php
/**
 * @property Merits $Merits
 */

class CategoriesController extends AppController {

    var $name = 'Categories';

    var $hasMany = array(
        'Merit' => array(
            'className'     => 'Merit',
            'foreignKey'    => 'category_id',
        )
    );
}

?>

---------------------------------

<?php
/**
 * @property Merits $Merits
 */

class MeritsController extends AppController {

    var $name = 'Merits';

    var $belongsTo = array(
        'Category' => array(
            'className'    => 'Category',
            'foreignKey'    => 'category_id'
        )
    );

    function index() {

        $this->layout = 'cv';

        $this->set('utbildning', $this->Merit->find('all', array(
            'conditions' => array('Merit.category_id' => 1),
            'order' => array('Merit.modified DESC')
        )));

    function edit($id = null) {
        $this->Merit->id = $id;
        if (empty($this->data)) {
            $this->data = $this->Merit->read();
        }
        else {
            if ($this->Merit->save($this->data)) {
                $this->Session->setFlash('Meriten har uppdaterats.');
                $this->redirect(array('controller' => 'merits',
'action' => 'index'));
            }
        }
    }
}
?>

Now to my issue. How do I do with the edit-action? It saves everything
to a single table, however I would like to have a
<select>
<option value="['Category']['id']">['Category']['name']</option>
<option value="['Category']['id']">['Category']['name']</option>
<option value="['Category']['id']">['Category']['name']</option>
</select>

and post the value to ['Merit']['categories_id'].

How do I do that?

On 23 Juli, 10:54, Joachim Nyqvist <[email protected]> wrote:
> No need to apologize about language. I was just lazy, so I didn't
> translate, I just wanted to show which columns where corresponding.
>
> Thanks for the answer. I will certainly look into this in detail when
> I have a little more time over.
>
> You're definitely right about dropping the join-table. As the setup is
> today, a record can belong to many categories, however that wasn't the
> intention. The intention was only one category per record. I don't
> really know why I made it more complicated. So "Category hasMany
> Record" and "Record belongsTo Category" makes perfect sense.
>
> On 22 Juli, 21:23, cricket <[email protected]> wrote:
>
> > On Thu, Jul 22, 2010 at 10:36 AM, Joachim Nyqvist <[email protected]> 
> > wrote:
> > > Ok, I'm new to Cake, but not php. I don't know how to build my models
> > > for easy creation of add, edit and delete actions.
>
> > > I have three tables:
>
> > > The records:
>
> > > CREATE TABLE merits (
> > >  meritId int(10) unsigned NOT NULL AUTO_INCREMENT,
> > >  ar varchar(20) DEFAULT NULL,
> > >  titel varchar(40) NOT NULL,
> > >  plats varchar(40) DEFAULT NULL,
> > >  stad varchar(40) DEFAULT NULL,
> > >  beskrivning text,
> > >  modifierad timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE
> > > CURRENT_TIMESTAMP,
> > >  PRIMARY KEY (meritId)
> > > ) ENGINE=InnoDB  DEFAULT CHARSET=latin1;
>
> > > The categories:
>
> > > CREATE TABLE kategoris (
> > >  kategoriId int(10) unsigned NOT NULL AUTO_INCREMENT,
> > >  kategoriNamn varchar(40) NOT NULL,
> > >  PRIMARY KEY (kategoriId)
> > > ) ENGINE=MyISAM  DEFAULT CHARSET=latin1;
>
> > > and the table to connect categories and the records:
>
> > > CREATE TABLE cvs (
> > >  kategoriId int(10) unsigned NOT NULL,
> > >  meritId int(10) unsigned NOT NULL,
> > >  PRIMARY KEY (kategoriId,meritId)
> > > ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
>
> > > For now I have managed to produce a index view with the following:
>
> > > <?php
> > > /**
> > >  * @property Cvs $Cvs
> > >  */
>
> > > class CvsController extends AppController {
>
> > >    var $name = 'Cvs';
> > >    var $helpers = array('Minify', 'Mailto');
>
> > >    function index() {
>
> > >        $this->layout = 'cv';
>
> > >        $this->set('utbildning', $this->Cv->query("SELECT merits.ar,
> > > merits.titel, merits.plats, merits.stad, merits.beskrivning FROM
> > > merits JOIN cvs ON merits.meritId = cvs.meritId JOIN kategoris ON
> > > cvs.kategoriId = kategoris.kategoriId WHERE kategoris.kategoriNamn =
> > > 'Utbildning' ORDER BY merits.modifierad DESC;"));
> > >    }
> > > }
> > > ?>
>
> > > A simple SQL-query, because I know how to work them.
>
> > > Now, I don't know how to write the models, so i can add the other
> > > actions, add, edit, delete. After some researches, I think it has
> > > something to do with (hasOne, hasMany, belongsTo) but would certainly
> > > need some help.
>
> > Yes, you should be following Cake's conventions so that you can take
> > advantage of associations (hasOne, etc.). However, I'm goingto have to
> > show you using english, simply because I'm unsure of the inflections.
> > Sorry about that. Maybe someone else can adjust this to use your
> > laguage.
>
> > First thing, use "id" for the PK:
>
> > CREATE TABLE merits (
> >         id int(10) unsigned NOT NULL AUTO_INCREMENT,
> >         ar varchar(20) DEFAULT NULL,
> >         titel varchar(40) NOT NULL,
> >         plats varchar(40) DEFAULT NULL,
> >         stad varchar(40) DEFAULT NULL,
> >         beskrivning text,
> >         modifierad timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE
> >         CURRENT_TIMESTAMP,
> >         PRIMARY KEY (meritId)
> > ) ENGINE=InnoDB  DEFAULT CHARSET=latin1;
>
> > For this table, normally we would use "name" also, so that
> > find('list') will work properly. To use "kategoriNamn" instead, you'll
> > need to change the $displayField in the model definition:
>
> > var $displayField = 'kategoriNamn';
>
> > You /can/ also change the PK for the model to "kategoriId" but that's
> > complicating things.
>
> > CREATE TABLE kategoris (
> >         id int(10) unsigned NOT NULL AUTO_INCREMENT,
> >         kategoriNamn varchar(40) NOT NULL,
> >         PRIMARY KEY (kategoriId)
> > ) ENGINE=MyISAM  DEFAULT CHARSET=latin1;
>
> > Normally, the join table name is made from the names ofthe two tables,
> > "kategoris_merits" in this case. Then, column names are based on the
> > singular tablename plus "_id". If your tables were "records" and
> > "categories", your table would be:
>
> > CREATE TABLE kategoris_merits (
> >         category_d int(10) unsigned NOT NULL,
> >         record_id int(10) unsigned NOT NULL,
> >         UNIQUE(category_d, record_id)
> > ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
>
> > This is where the inflections fget confusing for me. You see that the
> > column names are singular. I don't know how that would work for you.
>
> > Note that you don't make a multiple-column PK. You can add a UNIQUE
> > key, though, and, if you really want a PK, add an auto_increment
> > column, like with any table.
>
> > Once you have the tables set up, you then need to define the
> > associations in the models. Because you've got a join table, I assume
> > that you a Record can belong to more than one Category. In thatcase,
> > you need to set up HABTM associations in each model.
>
> > If a Record can only belong to a single Category, though, you should
> > drop the join table, add a category_id column to the records table,
> > and your assoc. would be:
>
> > Category hasMany Record
> > Record belongsTo Category
>
> > Again, I apologise for the lack of language-specific help. Perhaps all
> > this can be done without resorting to english names.

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en

Reply via email to