Welcome Guest, Not a member yet? Register   Sign In
datamapper ORM question
#1

[eluser]macron[/eluser]
Hi
I have a problem figuring out how to use the select and select_func with a relation

I have two tables like this:
news: id, date and user_id
news_text: id, headline, content, language and news_model_id

Code:
class News_model extends DataMapper
{
    var $table          = 'news';
    var $has_many       = array('image_model', 'news_text_model');
    var $created_field  = 'date';
}

class News_text_model Extends DataMapper{

    var $table          = 'news_text';
    var $has_one       = array('news_model');
}

In my controller I do this:
Code:
$news           = new News_model();
            //$news->select('id, user_id)->select_func("DATE_FORMAT", array('@date', '[,]', "'%e %M %Y'"), 'date') <-- this is not working!!!
            $news->order_by('date', 'DESC');
            $news->get_paged(1, 3);
            foreach($news as $item){
                $item->news_text_model->where('language', lang('db_column'))->get();
            }
            
            $data['news']   = $news;

This all works fine, but the date field from news is in ISO format so I added
Code:
$news->select('id, user_id)->select_func("DATE_FORMAT", array('@date', '[,]', "'%e %M %Y'"), 'date');

but this does not work like I expected. The news_model object is now ok, date formatted etc., but the related objects cant be fetched anymore - they are null if I try to echo them.
Why is that?

/Kaj
#2

[eluser]WanWizard[/eluser]
Start by adding
Code:
$news->check_last_query();
after your get_paged() call. When you see the query produced, you can probably spot what the culprit is.

Which I assume is your date field, because the code is ok, and so is the query produced.

How is your date column defined? If you run DATE_FORMAT() on a column that is not a date column, it returns NULL.




Theme © iAndrew 2016 - Forum software by © MyBB