Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Mustache don't work with Model::find()
  •   //Controller
       
        $this->template->set_global(array(
          'title' => 'Welcome',
          'books' => Model_Books::find('all'),
         
          'planets' => array(
            array(
              'nome' => 'Terra',
              'populacao' => '7 Bilhoes',
              'moeda' => 'Real'
            ),
            array(
              'nome' => 'Marte',
              'populacao' => '0 pessoas',
              'moeda' => 'Desconhecida'
            )
          )
        ));
       
        //View
       
        {{title}} => Welcome
         
        {{#planets}}      
         {{nome}}
          {{populacao}} => Show in array planets
          {{moeda}}
        {{/planets}}
         
        {{#books}}
         {{name}}
          {{author}} => Don't work
          {{text}}
        {{/books}}
     
        {{^books}}
          <p>Not match found!</p> => Too don't work
        {{/books}}

    how do I display the results from the model with the mustache?
  • HarroHarro
    Accepted Answer
    I don't know Mustache, but could it be it wants an array there?

    If so, you need to convert from objects to an array:

    if ($books = Model_Books::find('all'))
    {
        foreach($books as $key => $book)
        {
            $books[$key] = $book->to_array();
        }
    }

  • Harro... exactly, as the ORM returns an object.

    I'm trying to bring the results of a single user.
    When there are no results, the mustache is working \o/
    But it is still not bringing users containing results.

    image

    My controller.
  • HarroHarro
    Accepted Answer
    That will give you an array of arrays.

    As Mustache (if I understand the docs correctly) uses a section to access an array, you need nested sections, one for the outer array of all books, one for each book.

    {{#books}}
        {{#book}}
            {{name}}
            {{author}} => Don't work
            {{text}}
        {{/book}}
    {
    {/books}}

    If this fixes it, perhaps objects works too, I don't know how smart it is.
  • Sorry for the english ... I'm Brazilian :)
  • I made some changes...

    Controller:
      public function action_index()
      {
        $this->template->title = 'Bem-vindo';

        if ($books = Model_Books::find('all', array('where' => array(array('user_id', $this->current_user->id)))))
        {
          foreach($books as $key => $value)
          {
              $books = $value->to_array();
          }
        }

        var_dump(DB::last_query());

        $this->template->set_global('books', $books, false);   
        $this->template->set_global('name', Auth::get_profile_fields('name'));

        $this->template->content = View::forge('wibs/content.mustache');
      }

    View:
    <p>Olá {{{name}}} {{current_user.id}} <a href="user/logout">Logout</a></p>

    {{#books}}
      <p>{{name}}</p> =>
    Is bringing only one result of DB
    {{/books}}

    {{^books}}
      <p>Not match found!</p>  => Work \o/
    {{/books}}


    Thus also returns only one result from DB
    {{#books}}
      {{#books}}
      <p>{{name}}</P>
      {{/books}}
    {{/books}}

    My model:
    class Model_Books extends Orm\Model
    {
      protected static $_properties = array('id', 'name', 'pages', 'user_id');
      protected static $_primary_key = array('id');

    }

    Why did not return all results user?
  • [SOLVED]

    //Controller

      public function action_index()
      {
        $this->template->title = 'Bem-vindo';

        $this->template->set_global('books', Model_Books::get_books($this->current_user->id), false);   
        $this->template->set_global('name', Auth::get_profile_fields('name'));

        $this->template->content = View::forge('wibs/content.mustache');
      }

    //Function in Model

      public static function get_books($id)
      {
          $data = DB::select()
            ->from('books')
            ->where('user_id', $id)
            ->execute();

          return $data->as_array();
      }

    And everything working... :D
    Thank you for your help Harro.
  • kazumikazumi
    Accepted Answer
    E qual era o problema?
    What was the issue though?
  • Kazumi, the way he was doing, Model_Books :: find ('all'), the data showed no mustache. Why ORM returns as an object. So I did the query that way.

    $ data = DB :: select () -> from ('books') -> execute ();
    $ data-> to_array ();

    http://www.fuelphp.com/forums/discussion/comment/18604#Comment_18604

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion