Welcome Guest, Not a member yet? Register   Sign In
Get object from id
#1

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
Reply


Messages In This Thread
Get object from id - by reysing - 03-29-2018, 05:05 AM
RE: Get object from id - by php_rocs - 03-29-2018, 09:37 AM
RE: Get object from id - by InsiteFX - 03-30-2018, 04:02 AM
RE: Get object from id - by reysing - 04-03-2018, 06:23 AM
RE: Get object from id - by qury - 04-03-2018, 09:01 AM
RE: Get object from id - by reysing - 04-05-2018, 07:39 AM



Theme © iAndrew 2016 - Forum software by © MyBB