Tutorials
This page will list several tutorials found on this blog. Tutorials will be listed either based on what they cover or their difficulty level.
To get you started
Your new to Kohana, you know what it sort of is about but you’d like some help building your first application. You have found the user guide and you know a little about php.
The Blog tutorial is a good start I think. It introduces you to models, controllers and views.
April 26th, 2008 at 2:34 pm
Hi was wondering if the database metadata function list_fields() could have been used to list the tables;
public function __construct($id = NULL)
{
parent::__construct();
$this->data = $this->db->list_fields($this->table);
if ($id != NULL)
{
// try and get a row with this ID
$data = $this->db->getwhere($this->table_name, array('id' => $id))->result(FALSE);
// try and assign the data
if (count($data) == 1 AND $data = $data->current())
{
foreach ($data as $key => $value)
$this->data[$key] = $value;
}
}
}
Thanks for the Lib!!!!
April 26th, 2008 at 3:09 pm
Just realized that it not just field names but values / Database fields and default values hmm…
April 26th, 2008 at 6:27 pm
Yes, you can specify default values in your model as well =)
The only things that don’t work are things like ‘date’ => time(), since php doesn’t allow you to do that. You can accomplish this in your constructor though. Useful for things like blog posts where you want the post to have the current time.
April 27th, 2008 at 9:07 pm
Actually what I wanted to know was if there was an easier to set the table names other than manually like, using the metadata list fields function above does not work, is there any other way!!!
April 29th, 2008 at 7:21 pm
I was just wondering why is it in the add function it says $this->upload($post); but where is upload in the controller, is that an error or if not can you explain what it does?
April 29th, 2008 at 7:24 pm
Ah, that should be do_post(), sorry, a typo! In my controller I named it upload(), but I changed the name for the example.
April 30th, 2008 at 7:09 pm
Hi zombor,
one quick question, what if i just wished to update one field and not all the fields in my table?
it looks as if your save function updates all the fields (i could be mistaken), so if i hadn’t assigned values to all the keys in the data array it would assign the columns in the database table with an empty string.
am i mistaken?
April 30th, 2008 at 7:14 pm
When you do new My_Model($id), it assigns all the rows the value from the database, so if you update that model, it will retain what was in the row to begin with.
The default values are just there for creating new rows.
May 7th, 2008 at 10:07 am
There are advantages (not being restricted by table-naming conventions is one of them) to using your auto_modeler but the useful __call() function to map find_by_* calls is missing.
I’ve added the method but limited it to find_by_* queries.
May 27th, 2008 at 10:58 am
I believe function do_post() contains an error. Var $post is used for two different things. This is correct imho:
private function do_post($POST_DATA)
{
// Insert/update the new item magically
$post= new Post_Model();
$post->set_fields($POST_DATA);
$post->save();
}