ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

DataMapper 1.6.0

September 05, 2008 12:32pm

Subscribe [115]
  • #481 / Feb 05, 2009 4:43am

    Yman

    7 posts

    Hi guys,

    i hav a question here

    normally when we pass a query into “view”, we do:

    in controller/function
    $data[‘query’] = $this->db->get(‘entries’);

    in views (i use separate php for viewing)
    <html><....

    <?php
    foreach($query->result() as $row): ?>
    <li> <?=$row->watever?> </li>

    ...></html>

    but when i try using get() method , i cant seem to get it work:

    in controller/function
    $query = new Horizontal();
    $query->get();

    in views (i use separate php for viewing)
    <html><....

    <?php
    foreach ($query->all as $row){

    echo '<li>' . $row->watever . '</li>' .
    }

    it shows query not defined.

    how can I do it correctly?

  • #482 / Feb 05, 2009 5:54am

    OverZealous

    1030 posts

    @Yman
    Are you sure you are passing the $query into your view?  Is Horizontal a valid DataMapper class?  Where is this error being thrown (in the controller or the view)?

    You need to do some basic debugging here, because there is nothing that can be inferred from what you posted that would give you that error, unless you have a typo or other mistake in your code.

  • #483 / Feb 05, 2009 7:46am

    Yman

    7 posts

    ah , nvm i got it already

    $h = Object()
    $h->get()

    i just need to add this :

    $data[‘h’] = $h

  • #484 / Feb 08, 2009 1:20pm

    Daniel H

    197 posts

    ** Post removed - I am an idiot **

  • #485 / Feb 09, 2009 6:17am

    macigniter

    244 posts

    I am having the following issue. I get an error that my “id” field is ambiguous:

    Error Number: 1052
    
    Column 'id' in field list is ambiguous
    
    SELECT `id` FROM (`products`) LEFT JOIN `join_clients_products` ON `products`.`id` = `join_clients_products`.`product_id` LEFT JOIN `clients` ON `clients`.`id` = `join_clients_products`.`client_id` WHERE `clients`.`id` = 9

    This is my query. I can’t find anything wrong with it:

    $c = new Client();
    $c->get_by_id($id);
    
    $p = new Product();
    $p->select('id');
    $p->where_related_client('id', $id);
    $p->get();

    Any idea what I’m doing wrong?

  • #486 / Feb 09, 2009 7:45am

    macigniter

    244 posts

    Sorry for posting again. I’m new to the DM class and still learning… The following problem is just driving me crazy:

    In my application products can be assigned to clients by checking some checkboxes and saving the record. In the scenario where I remove all product relations from an existing client and then after saving add a product again, the client somehow gets a new user id although there are existing relations to other tables (e.g. users). Any idea why DM is increasing the client id?

    The update function looks like this:

    $c = new Client();
    $c->get_by_id($id);
    
    $p = new Product();
    $p->where_related_client('id', $id);
    $p->get();
    
    // delete old product selection for this client
    $c->delete($p->all);
    
    $c->name = $this->input->post('name');
    $c->descr = $this->input->post('descr');
    $products = $this->input->post('product');
    
    if (is_array($products))
    {
        $p = new Product();
        $p->where_in('id', $products);
        $p->get();
        $c->save($p->all);
    }
    else 
    {
        $c->save();
    }

    It looks like $c->delete($p->all) is where the problem is. I do find it strange though that even the relation from the client to the user table is being deleted… Any ideas?

    UPDATE: I found the bug. Since the error only occured after adding a new product after all product/client relations have been previously deleted I had to add “if ($p->exists())” before the delete to prevent DM running a deletion on an empty relation table.

    if ($p->exists()) $c->delete($p->all);

  • #487 / Feb 09, 2009 12:27pm

    OverZealous

    1030 posts

    I am having the following issue. I get an error that my “id” field is ambiguous <snip>

    There are two solutions to your problem.  The first is to modify the select to include the table name:

    $p->select('products.id');

    The second is to use the built-in relationships to generate your query, which will automatically add the table name:

    $c = new Client();
    $c->get_by_id($id);
    $p = $c->product->select('id')->get();

    If you don’t need anything more complex than what is listed, I recommend the second method for now.

    I’m working on a major update to DataMapper, and one of the changes is adding the table name to fields more often than it currently does (and only if the name hasn’t been added already!).

  • #488 / Feb 09, 2009 12:43pm

    macigniter

    244 posts

    Thanks Phil!

    Also, would it make sense to change the behavior of “delete” when trying to delete relations although there are no relations currently stored in the relation table? Referring to my earlier post I noticed that a ->delete on a non-existing relation caused DM to delete much more than I anticipated. It’d be great if I wouldn’t necessarily have to worry about the if ($p->exists()) before deleting relations.

  • #489 / Feb 09, 2009 12:47pm

    OverZealous

    1030 posts

    I’m not sure why that would be happening (it shouldn’t be).  I’ll look into it later, but if you have a moment, could you send the SQL being generated by DM for that specific delete?  There are several ways to get this, but I believe turning on CodeIgniter’s performance profiler is the easiest, using this line of code in your controller:

    $this->output->enable_profiler(TRUE);

    That will help me narrow it down.

  • #490 / Feb 09, 2009 1:39pm

    macigniter

    244 posts

    This is what output generates:

    SELECT * FROM `clients` LIMIT 1 
    
    SELECT *
    FROM (`clients`)
    WHERE `id` = 1 
    
    SELECT * FROM `products` LIMIT 1 
    
    SELECT `products`.*
    FROM (`products`)
    LEFT JOIN `join_clients_products` ON `products`.`id` = `join_clients_products`.`product_id`
    LEFT JOIN `clients` ON `clients`.`id` = `join_clients_products`.`client_id`
    WHERE `clients`.`id` = 1 
    
    // why is it doing the following deletes affecting other relations with the "clients" table?
    DELETE FROM `clients`
    WHERE `id` = '1' 
    
    SELECT * FROM `users` LIMIT 1 
    
    DELETE FROM `join_clients_users`
    WHERE `client_id` = '1' 
    
    DELETE FROM `join_clients_products`
    WHERE `client_id` = '1' 
    
    SELECT * FROM `files` LIMIT 1 
    
    DELETE FROM `join_clients_files`
    WHERE `client_id` = '1' 
    
    SELECT *
    FROM (`products`)
    WHERE `id` IN ('3', '5')  
    
    INSERT INTO `clients` (`name`, `descr`, `created`, `updated`) VALUES ('Test Client', '', '2009-02-09 17:28:14', '2009-02-09 17:28:14')
    
    SELECT *
    FROM (`join_clients_products`)
    WHERE `client_id` = 2
    AND `product_id` = '3' 
    
    INSERT INTO `join_clients_products` (`client_id`, `product_id`) VALUES (2, '3') 
    
    SELECT *
    FROM (`join_clients_products`)
    WHERE `client_id` = 2
    AND `product_id` = '5' 
    
    INSERT INTO `join_clients_products` (`client_id`, `product_id`) VALUES (2, '5') 
    
    SELECT *
    FROM (`clients`)
  • #491 / Feb 09, 2009 2:08pm

    OverZealous

    1030 posts

    I figured out why.  It makes sense, although I’ll need to discuss with stensi how he’d like to solve it.

    Basically, the test to see if $object->delete($other_object) should delete the provided $other_object or the whole $object itself checks to see if $other_object is empty.  If you pass in an empty array, that’s the same thing as passing in nothing.

    The short-term solution, for now, is to check to see if $products->all is empty first, and only delete if it isn’t:

    if ( ! empty($products->all))
    {
        $client->delete($products->all);
    }

    Note: I realize this is basically what you are doing, but more “accurate”.

    Since DataMapper is not my project, I’ll pass this on to stensi.

  • #492 / Feb 09, 2009 3:51pm

    macigniter

    244 posts

    Thanks!

    I feel bad to ask another question. But it’s a hair-puller…

    I have a “Login” controller and want to instantiate a “Login” object of my “Login” Model. Somehow I can’t get it to work. Is that a namespace issue?

    Controller:

    <?php
        class Login extends Controller {
    
        function Login()
        {
            parent::Controller();
        }
        
        function index()
        {
                    [...]
    
                $u = new User();
                     
            $u->username = $this->input->post('user');
            $u->password = $this->input->post('pass');
                        
            if ($u->login())
            {
                 $l = new Login();
                 // this is where the output stops
                 // even if I put this in the model method login()
            }
        }

    How do I have to call the model? Or is it not possible to name a model with the same name as an existing controller?

  • #493 / Feb 09, 2009 3:58pm

    OverZealous

    1030 posts

    Yes.  Obviously, if both PHP Classes are named Login, only one Login class can exist.  You’ll need to rename your Login model.

    Maybe more importantly, are you sure it’s a model you want for that?  Models are meant to store data (to and from the database).

    You should use a Library to place all of your login code in one location, that can easily be loaded and used as needed.  I have a Login_Manager library that I use for this purpose.  You just create a Login_manager class, place it in libraries, and call

    // inside my controller
    $this->load->libary('login_manager');
    $this->login_manager->process_login($user);

    If you need to access it from within a model, you’ll need to be more creative, although I recommend keeping your business code separate from your model code whenever possible.

    // only within a model
    // get CodeIgniter
    $CI =& get_instance();
    $CI->load->library('login_manager');
    $CI->login_manager->process_login($this);

    Finally, CodeIgniter lets you do some creative things with routing controllers.  If you choose, you could create a controller called Access_control (for example), but add routes for /login and /logout, etc, to your routes.php under config/.

  • #494 / Feb 09, 2009 4:05pm

    macigniter

    244 posts

    Thank you so much Phil. I really appreciate your patience 😊

    My login model actually stores the user “logins”. It’s not the login management. It contains the number of logins, browser, ip address etc. of a logged-in user. So I guess I just have to rename it…

  • #495 / Feb 09, 2009 4:07pm

    OverZealous

    1030 posts

    A-Ha!  That makes more sense!

    Yes, that’s a basic “problem” with OOP code.  Sometimes, you just have to be more verbose with your naming.

    (How ‘bout Accesslog? 😉 )

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases