Quantcast
Channel: CodeIgniter Forums - All Forums
Viewing all articles
Browse latest Browse all 14343

Get object from id

$
0
0
Hello,

 I've two table with a relation one-to-many 

Code:
(players->statistics)

.

I get all the stats with this method :
Code:
return $this->db->get('statistics')->result_array();

Which means I also get the player_id in there, but I can get it as a string and not as an object.
What I wanted to do is this (Twig) :
Code:
{{stat.player_id.name}}

But that doesn't work as it's not an object.


I heard that I can do that with Join queries so I did this :

Here's my function in Statistics_model.php :
Code:
function get_all_statistics()
{
   $this->db->select('*');
   $this->db->from('statistics');
   $this->db->join('joueurs', 'joueurs.id = statistics.player_id');
   return $query = $this->db->get()->result();
}

Index action in the controller :
Code:
function index()
{
   $data['statistics'] = $this->Statistic_model->get_all_statistics();

   $this->load->library('twig');
   $this->twig->display('statistics/index', $data);

}

And finally the view :
Code:
{% for stat in statistics %}
   <tr>
       <td>{{stat.stat_id}}</td>
       <td>{{stat.leg_id}}</td>      
       <td>{{stat.player_id.name}}</td>
       <td>{{stat.darts}}</td>
       <td>{{stat.finish}}</td>
       <td>{{stat.reste}}</td>
       <td>{{stat.max}}</td>
   </tr>
{% endfor %}

Everyhting works perfectly unless the stat.player_id.name, I cannot get it

Viewing all articles
Browse latest Browse all 14343

Trending Articles