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
#2

@reysing,

Are you sure that it is stat.player_id.name and not stat.name or stat.player_id? All you need to do look at the query results and see the column name (that you should be using).
Reply
#3

PHP Code:
var_dump($data); 

That will show you what is in it.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

(03-30-2018, 04:02 AM)InsiteFX Wrote:
PHP Code:
var_dump($data); 

That will show you what is in it.

Ok so I have this :

array(1) { ["statistics"]=> array(1) { [0]=> object(stdClass)#20 (7) { 
["stat_id"]=> string(1) "2" 
["leg_id"]=> string(1) "1" 
["player_id"]=> string(1) "1" 
["darts"]=> string(2) "24" 
["finish"]=> string(3) "101" 
["reste"]=> string(1) "0" 
["max"]=> string(1) "1" } }
}

So I have player_id = 1. How do I get the name, etc. from this player1 ?
Reply
#5

I do not have your table layout, but i believe you have a table called joueurs and in there a field called name.

My understanding is that if you do not specify the field names   select('*')  , the sql query will return  all the column headers.

You could do this:
PHP Code:
function get_all_statistics()
{
 
  $this->db->select([
 
                 'statistics.stat_id',
 
                 'statistics.leg_id',
 
                 'statistics.darts',
 
                 'statistics.finish',
 
                 'statistics.reste',
 
                 'statistics.max',
 
                 'joueurs.id',
 
                 'joueurs.name'
]);
 
  $this->db->from('statistics');
 
  $this->db->join('joueurs''joueurs.id = statistics.player_id');
 
  return $query $this->db->get()->result();


Then in your view you should simply refer to them as:
PHP Code:
{% for stat in statistics %}
 
  <tr>
 
      <td>{{stat.stat_id}}</td>
 
      <td>{{stat.leg_id}}</td      
       
<td>{{stat.name}}</td>
 
      <td>{{stat.darts}}</td>
 
      <td>{{stat.finish}}</td>
 
      <td>{{stat.reste}}</td>
 
      <td>{{stat.max}}</td>
 
  </tr>
{% endfor %} 
Reply
#6

(04-03-2018, 09:01 AM)qury Wrote: I do not have your table layout, but i believe you have a table called joueurs and in there a field called name.

My understanding is that if you do not specify the field names   select('*')  , the sql query will return  all the column headers.

You could do this:
PHP Code:
function get_all_statistics()
{
 
  $this->db->select([
 
                 'statistics.stat_id',
 
                 'statistics.leg_id',
 
                 'statistics.darts',
 
                 'statistics.finish',
 
                 'statistics.reste',
 
                 'statistics.max',
 
                 'joueurs.id',
 
                 'joueurs.name'
]);
 
  $this->db->from('statistics');
 
  $this->db->join('joueurs''joueurs.id = statistics.player_id');
 
  return $query $this->db->get()->result();


Then in your view you should simply refer to them as:
PHP Code:
{% for stat in statistics %}
 
  <tr>
 
      <td>{{stat.stat_id}}</td>
 
      <td>{{stat.leg_id}}</td      
       
<td>{{stat.name}}</td>
 
      <td>{{stat.darts}}</td>
 
      <td>{{stat.finish}}</td>
 
      <td>{{stat.reste}}</td>
 
      <td>{{stat.max}}</td>
 
  </tr>
{% endfor %} 

Oh no I'm so dumb  Big Grin. I was stuck 1 week for such a stupid thing lol. Thank you very much. Yes, just using stat.name works.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB