Welcome Guest, Not a member yet? Register   Sign In
clear active record
#1

[eluser]Clifford James[/eluser]
I'm building an ORM for CI using CI's Active Record.

In a few methods I'm using $this->db->select('table.*'); but this gives an error while working with multiple relations in one action (cleaning up relations after one is deleted).

The problem is that $this->db->select() is called multiple times and my query looks like this:
SELECT `contacts`.*, `contacts`.*, `orders`.* FROM (`orders`) WHERE `orders`.`customer_id` = 3

How can I clear previous Active Record settings (e.g. table, where, join etc.)?
#2

[eluser]Cesar Kohl[/eluser]
To load everything on a table, use this code:

Code:
$query = $this->db->get('mytable');

Read the User Guide to have more information: http://ellislab.com/codeigniter/user-gui...ecord.html

You need to put the query in different strings:

Code:
$author = 'Cesar';
$this->db->select('id_author');
$this->db->where('name',$author);
$query_id_author = $this->db->get('authors');

$query_id_author->result();

$this->db->select('id_author');
$this->db->where('lastname','Kohl');
$query_id_author2 = $this->db->get('authors');

$query_id_author2->result();

At least is what I do and I never had your problem.
#3

[eluser]Clifford James[/eluser]
Removed the ->select('table.*') works for now, but it's weird it is used again after ->get() , ->result() , ->row() , etc don't you agree?
#4

[eluser]Clifford James[/eluser]
[quote author="Cesar Kohl" date="1292354773"]To load everything on a table, use this code:

Code:
$query = $this->db->get('mytable');

Read the User Guide to have more information: http://ellislab.com/codeigniter/user-gui...ecord.html

You need to put the query in different strings:

Code:
$author = 'Cesar';
$this->db->select('id_author');
$this->db->where('name',$author);
$query_id_author = $this->db->get('authors');

$query_id_author->result();

$this->db->select('id_author');
$this->db->where('lastname','Kohl');
$query_id_author2 = $this->db->get('authors');

$query_id_author2->result();

At least is what I do and I never had your problem.[/quote]

What do you see in the queries section in the profiler after these queries?
#5

[eluser]Clifford James[/eluser]
Here an example:

Code:
$this->db->select('contact.*');
$this->db->where(array('id' => 1));
$contact = $this->db->get('contacts')->row();

//SELECT `contacts`.* FROM (`contacts`) WHERE `contacts`.`id` = 1

$this->db->select('contact.*');
$this->db->where(array('id' => 1));
$contact = $this->db->get('contacts')->row();

//SELECT `contacts`.*, `contacts`.* FROM (`contacts`) WHERE `contacts`.`id` = 2

$this->db->select('customer.*');
$this->db->where(array('id' => 1));
$customer = $this->db->get('customers')->row();

//SELECT `contacts`.*, `contacts`.*, `customers`.* FROM (`customers`) WHERE `customer`.`id` = 1
#6

[eluser]Cesar Kohl[/eluser]
I didn't understand your example, so I'll use another one:

Code in controller:

Code:
//Assume there is a table with 4 rows: id, phrase, tags, id_author
        echo '<pre>';
        
        //Selects the row 'phrase' with 'id=24' on table 'phrases'
        $this->db->select('phrase');
        $this->db->where('id',24);
        $contact1 = $this->db->get('phrases');
        
        print_r($contact1->row_array());
        
        
        //Selects rows 'id' and 'phrase' with 'id=24' on table 'phrases'
        $this->db->select('id,phrase');
        $this->db->where('id',24);
        $contact2 = $this->db->get('phrases');
        
        print_r($contact2->row_array());
        
        
        //Selects all rows with 'id=24' on table 'phrases'
        $this->db->select('');//this line can be deleted
        $this->db->where('id',24);
        $contact3 = $this->db->get('phrases');
        
        print_r($contact3->row_array());

Output in browser:

Quote:Array
(

[phrase] => The truth is you don't know what is going to happen tomorrow. Life is a crazy ride, and nothing is guaranteed.
)

Array
(

[id] => 24
[phrase] => The truth is you don't know what is going to happen tomorrow. Life is a crazy ride, and nothing is guaranteed.
)

Array
(

[id] => 24
[phrase] => The truth is you don't know what is going to happen tomorrow. Life is a crazy ride, and nothing is guaranteed.
[tags] => truth life
[id_author] => 22
)

Any questions?
#7

[eluser]Clifford James[/eluser]
The example is what my queries look like in the profiler, but in my orm I don't store the results in an array but I just return them.
#8

[eluser]Clifford James[/eluser]
Problem solved.

Moved the ->select() lower in the ORM method.
#9

[eluser]Cesar Kohl[/eluser]
I see I couln't help you much but, anyway, you solved the problem!
#10

[eluser]Clifford James[/eluser]
[quote author="Cesar Kohl" date="1292363183"]I see I couln't help you much but, anyway, you solved the problem![/quote]

Thanks for your time!




Theme © iAndrew 2016 - Forum software by © MyBB