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.

DMZ 1.7.1 (DataMapper OverZealous Edition)

March 14, 2010 11:43pm

Subscribe [104]
  • #181 / Apr 22, 2010 1:21pm

    Benedikt

    85 posts

    ... Just found out that there is a “or_where_related”-function.

    Sorry, missed that.

  • #182 / Apr 23, 2010 4:15am

    rideearthtom

    21 posts

    Still having issues with subqueries, as posted before (simplified with other clauses removed):

    $accepted_documents = new Document();
    $accepted_documents->select('id')
        ->where_related('sme_profile', 'id', $this->sme_profile->id);
    $documents = new Document();
    $documents->where_not_in_subquery('id', $accepted_documents);
    return $documents->count() > 0 ? TRUE : FALSE;

    Gives me this:

    A Database Error Occurred

    Error Number: 1054

    Unknown column ‘tdb_documents_subquery.id’ in ‘field list’

    I also tried

    $documents->where_related('sme_profile', 'id !=', $this->sme_profile->id);

    but this just returns zero rows, despite there being no related sme_profile rows with the given ID.

    Here’s the full generated query:

    SELECT COUNT(*) AS `numrows`
    FROM (`tdb_documents`)
    LEFT OUTER JOIN `tdb_join_documents_products` tdb_join_documents_products ON `tdb_documents`.`id` = `tdb_join_documents_products`.`document_id`
    LEFT OUTER JOIN `tdb_products` tdb_products ON `tdb_products`.`id` = `tdb_join_documents_products`.`product_id`
    LEFT OUTER JOIN `tdb_join_products_purchases` product_tdb_join_products_purchases ON `tdb_products`.`id` = `product_tdb_join_products_purchases`.`product_id`
    LEFT OUTER JOIN `tdb_purchases` product_tdb_purchases ON `product_tdb_purchases`.`id` = `product_tdb_join_products_purchases`.`purchase_id`
    LEFT OUTER JOIN `tdb_join_documents_locations_projects` tdb_join_documents_locations_projects ON `tdb_documents`.`id` = `tdb_join_documents_locations_projects`.`document_id`
    LEFT OUTER JOIN `tdb_join_documents_sme_profiles` tdb_join_documents_sme_profiles ON `tdb_documents`.`id` = `tdb_join_documents_sme_profiles`.`document_id`
    LEFT OUTER JOIN `tdb_sme_profiles` tdb_sme_profiles ON `tdb_sme_profiles`.`id` = `tdb_join_documents_sme_profiles`.`sme_profile_id`
    WHERE  (
    `product_tdb_purchases`.`sme_profile_id` = 1
    OR tdb_join_documents_locations_projects.id IN (SELECT `tdb_join_documents_locations_projects`.`id`
        FROM (`tdb_join_documents_locations_projects`)
        LEFT OUTER JOIN `tdb_projects` tdb_projects ON `tdb_projects`.`id` = `tdb_join_documents_locations_projects`.`project_id`
        LEFT OUTER JOIN `tdb_join_projects_purchases` project_tdb_join_projects_purchases ON `tdb_projects`.`id` = `project_tdb_join_projects_purchases`.`project_id`
        LEFT OUTER JOIN `tdb_purchases` project_tdb_purchases ON `project_tdb_purchases`.`id` = `project_tdb_join_projects_purchases`.`purchase_id`
        WHERE `project_tdb_purchases`.`sme_profile_id` = 1
        AND `tdb_join_documents_locations_projects`.`location_id` = '1')
    )
    AND `tdb_sme_profiles`.`id` != 1

    There are no related sme_profiles with ID=1, but remove the final WHERE and I get 8 rows returned.

    So basically there are two ways to get the result set I want - the subquery way and the join way - and neither of them work as expected, although I imagine the join way is to do with my query, not DMZ. But it looks like the subquery problems is a DMZ bug.

    EDIT: Just realised why the join method won’t work - it’s because I want to select all documents WITHOUT a related sme_profile where ID = 1. This query will select documents which HAVE a related sme_profile where id != 1. Hence zero rows.

    The idea is that an sme_profile will be related to zero-many documents, and I want to select this set’s relative complement which also satisfies numerous other conditions.

    Looks like the non-functional subquery is my only option that fits the DMZ way of doing things. I guess I’m going back to building an array of document IDs elsewhere and passing it to where_not_in().

  • #183 / Apr 23, 2010 7:33am

    modano

    32 posts

    Hello,
    Im having a bit of trouble getting my head around Many-to-Many realtionships in DMZ.
    This is what I got,
    Many Users can have Many movies.

    In user model I got this:

    var $has_many = array(
            'movies' => array(
                'class' => 'movie',
                'other_field' => 'movie_id'
            )
        );

    And in movie model I got this:

    var $has_many = array(
            'users' => array(
                'class' => 'user',
                'other_field' => 'user_id'
            )
        );

    Is that correct? Is it enough info for it to write to the table users_movies (can the join table be named movies_users ?)

    Second question,
    In a html form where I create new USERS, i have a multi select list where one can choose movies. As it is a multi select, it will give php an array, so it has to be named movies[] doesnt it? How do I get that into the relationship in the model?

    Right, time to continue trying but would be grateful for any help,
    modano

  • #184 / Apr 23, 2010 8:05am

    OverZealous

    1030 posts

    @rideearthtom

    The problem may be using count, which has some oddness to it.  Have you tried using ->get(1) (limit 1), and then check result_count, to see if that works for you?


    modano
    Not to be rude, but everything you are asking is explained in the manual, as well as there are numerous examples in the manual AND in the example app.  There is no reason for the advanced relationships format with a normal many-to-many join.  Please read the pages under Getting Started first.  Also, don’t name your fields users or movies, just name them user and movie.  It tends to break things otherwise.

    The HTMLForm extension is no longer supported.

  • #185 / Apr 23, 2010 8:10am

    modano

    32 posts

    I have spent lots of time reading the manual (believe i have read it all), and it is great, not saying different. Im sorry but i just couldnt get my head around it.
    Will give it another go 😊

  • #186 / Apr 23, 2010 8:58am

    modano

    32 posts

    *edit*
    Sorted. sorry 😊

  • #187 / Apr 25, 2010 6:47pm

    parsifal

    1 posts

    I updated to_array() to turn related objects into nested data (rather than arrays of IDs)—and automatically loads related tables’ data if you don’t specify fields when calling to_array().

    Updated array.php attached (in a ZIP file).

    Hope this helps you as much as it helps me!
    Sean Gilbertson

  • #188 / Apr 27, 2010 1:05am

    PoetaWD

    97 posts

    Hello Phil,

    This is kind a advanced problem that I cannot solve since I am not that good with SQL syntax…

    My problem is that I want to perform a search in the entire object so I did like you said:

    //Check if Search data is sent
                if($this->input->post('sSearch'))
                {                
                    //Initiate a new SQL Query group
                    $obj->group_start();
                    
                    //Create a loop to go over each column
                    foreach($columns as $position => $column)
                    {
    
                        //Add a the SQL statment for searching in that column to the Query Group
                        $obj->or_like($column, $this->input->post('sSearch'));
                    }
                    
                    //Close the SQL Query Group 
                    $obj->group_end();
                }

    It will perform a search in every column of the table (object).

    My problem is when I include related objects…

    Is it possible to search in there also using the above function?

    It gives me this error when I try it:

    A Database Error Occurred
    
    Error Number: 1054
    
    Unknown column 'avaliator_name' in 'where clause'
    
    SELECT `browsers`.*, `avaliators`.`name` AS avaliator_name FROM (`browsers`) LEFT OUTER JOIN `avaliators` avaliators ON `avaliators`.`id` = `browsers`.`avaliator_id` WHERE ( `browsers`.`engine` LIKE '% Microso%' OR `browsers`.`browser` LIKE '% Microso%' OR `browsers`.`platform` LIKE '% Microso%' OR `browsers`.`version` LIKE '% Microso%' OR `avaliator_name` LIKE '% Microso%' ) ORDER BY `browsers`.`engine` asc

    Is there a way of making it happen ?

    Also, I am able to sort by the include_related column….. why not searching ?

    Thanks.

  • #189 / Apr 27, 2010 1:11am

    OverZealous

    1030 posts

    @PoetaWD
    If you want to search related items, you need to use or_like_related():

    //Check if Search data is sent
    if($this->input->post('sSearch'))
    {                
        //Initiate a new SQL Query group
        $obj->group_start();
                    
        //Create a loop to go over each column
        foreach($columns as $position => $column)
        {
            //Add a the SQL statment for searching in that column to the Query Group
            $obj->or_like($column, $this->input->post('sSearch'));
        }
    
        // You need some way to know which related object to look at
        // Here, I've chosen to have an array of related objects and their columns.
        foreach($related as $related_object => $columns)
        {
            foreach($columns as $column) {
                //Add a the SQL statment for searching in that column to the Query Group
                $obj->or_like_related($related_object, $column, $this->input->post('sSearch'));
            }
        }
               
        //Close the SQL Query Group
        $obj->group_end();
    }

    Hopefully that helps.

  • #190 / Apr 27, 2010 1:52am

    PoetaWD

    97 posts

    WHOA !

    Thanks man ! I did NOT know that it was a function just for that !!

    EDIT: Does this work for deep relationships ????

    BTW, how is the new job going ?

    I am still developing that code to generate grid data on-the-fly !

    Here is how the code works:

    //Variable with columns of the table
        var TableColums = [{ "sName": "engine" },
                           { "sName": "browser" },
                           { "sName": "platform" },
                           { "sName": "version" },
                           { "sName": "avaliator_name" } //Deep relationship column
                          ];
        
        //Initiate the table
            //The paramters are:
            //The id of the table container, the object to be loaded, the columns of the table
        
            initTableGrid("#browser", 'browser', TableColums);

    That is it ! You will have a NICE working table-grid working just by addin that lines of code in the HTML…. no php coding required !

    My next step is finding a way to add where clauses in the object query by the javascript….  :D

  • #191 / Apr 27, 2010 2:41am

    PoetaWD

    97 posts

    Does it work for deep relationships ? Like this:

    $obj->or_like_related('avaliator/company/manager', 'name', $this->input->post('sSearch'));

    Sorry for double posting… I just wanted to add some more information ....

  • #192 / Apr 27, 2010 9:33am

    OverZealous

    1030 posts

    @PoetaWD

    Yes.  It’s just part of Get Advanced.  Most normal query clauses can have _related appended to them, and you get a related query, instead of a simple column query.  Deep relationships are supported for most query clauses.

    The new job is going well.  It’s not exactly what I expected, but my coworkers are extremely nice.

  • #193 / Apr 27, 2010 2:41pm

    PoetaWD

    97 posts

    Thanks Phil !

    It works perfectly! DMZ impresses me more and more every day ! What is missing for it to be perfect ? (This is not a question… it is just a thought!)

    You dont sound very excited about your job…. but ... good to know that you like your new fellows…

  • #194 / Apr 28, 2010 6:18am

    ianbborg

    3 posts

    Hi there, I’m developing my first application with DMZ datamapper and it does save me alot of time so thanks for taking the time to develop it.

    I’m having a problem with relationships, are the models of a relationship auto-loaded? or do I need to load them in the constructor… Without loading the models I get this error message from php -

    Fatal error: Class 'town' not found in C:\xampp\htdocs\NEWSDT\sdt\application\libraries\datamapper.php on line 4472

    I have searched everywhere and found no solution but in the examples the models of a relationship arent loaded.

  • #195 / Apr 28, 2010 7:46am

    ianbborg

    3 posts

    Solved this problem. I was developing a module in the modules folder and the models where in the modules folder. So I setup a config var named $config[‘model_path’]; which is then added to the path of the autoload function like this:

    $file = $path . DataMapper::$config['models_path'] . 'models/' . $class . EXT;


    Now I have another problem with the relationships, table names seem to be duplicated in the query made by DMZ DM Why is this happening(maybe incorrect relationships) :

    Table 'sdt.general_county_general_towns_country' doesn't exist
    
    SELECT general_county.* FROM (general_county) LEFT OUTER JOIN general_county_general_towns_country general_county_general_towns_country ON general_county.id = general_county_general_towns_country.county_id WHERE `general_county_general_towns_country`.`townscountry_id` = 4


    Proper Table names are
    1.general_towns_country
    2.general_country
    3.town
    4.county

    1.general_towns_country Model

    class TownsCountry extends DataMapper
    {
        public $table ="general_towns_country";    
        
        /*var $has_one = array('town'=>array('class'=>'town',
                                           'other_field'=>'tid'
                                           ),
                            'general_country'=>array('class'=>'country',
                                             'other_field'=>'countryid'
                                           ),
                            'general_county'=>array('class'=>'county',
                                            'other_field'=>'cid'
                                           )
                                                        
                                                        );*/
        public $has_one = array('town','country','county');
    
        public function __construct()
        {
            // model constructor
            parent::__construct();    
             
        }
        
    }


    2.general_country Model

    class Country extends DataMapper
    {
        public $table ="general_country";    
        /*public $has_many = array('countryid'=>array('class'=>'townscountry',
                                                       'other_field'=>'general_country'                                                         
                                                          )
                                );*/
        
        public $has_many = array('townscountry');
        public function __construct()
        {
            // model constructor
            parent::__construct();
        }
        
    }

    3.town Model

    class Town extends DataMapper
    {
        public $table ="town";    
        /*public $has_many = array('tid'=>array('class'=>'townscountry',
                                                       'other_field'=>'town'                                                         
                                                          )
                                );*/
        public $has_many = array('townscountry');
    
        public function __construct()
        {
            // model constructor
            parent::__construct();
            
        }
        
    }

    4.general_county Model

    class County extends DataMapper
    {
        public $table ="general_county";    
    /*public $has_many = array('cid'=>array('class'=>'townscountry',
                                                       'other_field'=>'general_county'                                                         
                                                          )
                                );*/
        var $has_many = array('townscountry');
                                
        public function __construct()
        {
            // model constructor
            parent::__construct();
        }
        
    }
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases