ikkez / fabulog

the new F3-powered awesome fabulous blog-ware
GNU General Public License v3.0
62 stars 22 forks source link

How to run custom query like db->exec() in a controller? #6

Closed RNKushwaha closed 9 years ago

RNKushwaha commented 9 years ago

Hi,

I'm trying to develop a dropdown list of published pages when creating a new page. I just want to assign a parent page for each page. How can I show a list of all published pages excluding the current page and assign it to the current page(like an about us, contact us page under about page)?

When I write this code it show error in page_edit.htm Internal Server Error Object of class Model\Page could not be converted to string

        $parent = new \Model\Page();
        $parent->filter('page',array('published = ? and publish_date <= ?', true, date('Y-m-d')));
        $parent->countRel('page');
        $this->response->data['parents'] = $parent->find(null,array('order'=>'title asc'));;
        $f3->set('parents',$this->resource->exec("SELECT title FROM fblg_pages where id!='".$params['id']."' and published=1 order by title asc"));

When I tried this code

   $f3->set('parents',$this->resource->exec("SELECT title FROM fblg_pages where id!='".$params['id']."' and published=1 order by title asc"));

It show me error- Fatal error: Call to undefined method Model\Page::exec()

How can I call exec in a controller?

doonge commented 9 years ago

Model\Page is a Db\Cortex object, which is extended from the Db\Cursor class. Exec() exists in the Db\Sql class, not in the Db\Cursor nor DB\Cortex (which try to remain database system agnostic I think?). If you have an sql database active, try $db->exec('your sql statement')?

I don't think your problem is tied to the fact your code is inside a controller.

ikkez commented 9 years ago

Equivalent to your parents would be:

  $f3->set('parents', $parent->find(array('_id != ? and published = ?',$params['id'],1),array('order'=>'title asc')));
RNKushwaha commented 9 years ago

@ikkez its working now. Thanks