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