Welcome Guest, Not a member yet? Register   Sign In
i need help, how to merge ... newbie :)
#1

[eluser]amrnt[/eluser]
how to merge 2 tables the first table called users and has id,name, and some other columns, the other table is the actions, and has id, user_id, action ...

so user has many actions... how to get merge with less number of sql queries?
#2

[eluser]Pascal Kriete[/eluser]
Do you want to merge them physically - i.e. make a new table with all the data. Or do you want to select from both tables, joining them on the user_id ?
#3

[eluser]amrnt[/eluser]
joining them.
#4

[eluser]amrnt[/eluser]
i'm now reading about activerecords class,
is there like rails joining ; has_many, belongs_to, has_and_belongs_to_many
#5

[eluser]Pascal Kriete[/eluser]
You need to add a where statement to your query:
Code:
$this->db->select('users.*, actions.action');
$this->db->where('users.id', $some_id_from_input);
$this->db->where('users.id', 'actions.user_id', FALSE); // the important bit =)

$this->db->get('users');
Obviously you'll need to change things to fit your setup, but that's the basic idea.

Let us know how that goes.
#6

[eluser]Pascal Kriete[/eluser]
Whoops, missed your last reply. There are no orm functions, but if you search the forums for 'orm' there are a few libraries out there.
#7

[eluser]amrnt[/eluser]
thanks, but what i need is :

User_model
Code:
function get_name($id = null)
{
$this->db->where('id', $id);
$query = $this->db->get('users');
$user = $query->row();
return $user->name;
}

View
Code:
foreach($actions as $action):
echo $this->user_model->get_name($action->user_id);// here is what i want to edit to fit my wants, because when view is rendered as foreach as done queries made
endforeach;
[code]

QUERIES
[code]
0.0005      SELECT * FROM (`users`) WHERE `id` = '2'
0.0005      SELECT * FROM (`users`) WHERE `id` = '3'
0.0006      SELECT * FROM (`users`) WHERE `id` = '1'
0.0005      SELECT * FROM (`users`) WHERE `id` = '3'
0.0006      SELECT * FROM (`users`) WHERE `id` = '2'
0.0006      SELECT * FROM (`users`) WHERE `id` = '3'
0.0005      SELECT * FROM (`users`) WHERE `id` = '1'
0.0006      SELECT * FROM (`users`) WHERE `id` = '3'
0.0005      SELECT * FROM (`users`) WHERE `id` = '3'
0.0006      SELECT * FROM (`users`) WHERE `id` = '3'
0.0006      SELECT * FROM (`users`) WHERE `id` = '2'
0.0004      SELECT * FROM (`users`) WHERE `id` = '3'
0.0006      SELECT * FROM (`users`) WHERE `id` = '1'
0.0004      SELECT * FROM (`users`) WHERE `id` = '3'
0.0005      SELECT * FROM (`users`) WHERE `id` = '3'
0.0004      SELECT * FROM (`users`) WHERE `id` = '3'
0.0005      SELECT * FROM (`users`) WHERE `id` = '2'
0.0004      SELECT * FROM (`users`) WHERE `id` = '3'
0.0005      SELECT * FROM (`users`) WHERE `id` = '1'
0.0004      SELECT * FROM (`users`) WHERE `id` = '3'
#8

[eluser]amrnt[/eluser]
i want to be sth like
Code:
echo $action->User->name;
I dont know -_-
#9

[eluser]hvalente13[/eluser]
Hi,

What you want to do is like to put a pulley to make things go faster!

Active record it's capable of assist you to make what you want to do! And what I understood is that you want an header with user info and below the user actions!

1. Simple: Make two queries! I've read somewhere that separate queries give faster results! Give it a try.

2. Complex: Subqueries! This way you have to make sql string by hand and then you can retrieve it by:

Code:
$sql_strin = "SELECT * FROM ..." // you know what you want to do
$query = $this->db->query($sql_string);

This way the results will be all in the same row.

Hope this helpS
#10

[eluser]amrnt[/eluser]
[quote author="hvalente13" date="1218236624"].....
1. Simple: Make two queries! I've read somewhere that separate queries give faster results! Give it a try.
....[/quote]

Thanks, i'm using now separate queries Smile




Theme © iAndrew 2016 - Forum software by © MyBB