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]
  • #811 / Jan 08, 2011 5:50pm

    Dennis Rasmussen

    601 posts

    You only have to set $model if there are complications with the plural version of the model name. (unless I’m completely mistaken?)

  • #812 / Jan 08, 2011 6:42pm

    WanWizard

    4475 posts

    No, the other way around.

    $this->model is by default set to singular(classname). You have to set it manually if this fails to determine the proper singular name. And it has to be lowercase if you manually set it.

  • #813 / Jan 09, 2011 7:21pm

    modano

    32 posts

    Gi guys,

    I got a little problem with relationships that Ive been stuck on for a good few hours now and have run out of ideas, perhaps someone can help me out here. Im trying to get multiple Relationships to the Same Model to work:

    Tables:
    POSTS(id,source_id,courtesy_id)
    SOURCES(id,name)

    A post (blog post) has a source where the actual content might come from, but it also has an image in it, that comes from somewhere, and i have called it “courtesy_id”.
    For example, you write an article about foo and reference the source to be foo.com and in the article you put an image as well, and the image was found on site bar.com
    Now i can at the end say: Source: foo.com and image courtesy of: bar.com

    This is how I have set up the relationships:
    In POST model:

    var $has_one = array(
       'courtesy' => array(
          'class' => 'source',
          'other_field' => 'courtesy'
       ),
       'source' => array(
          'class' => 'source',
          'other_field' => 'source'
       )
    );

    and in SOURCE mode:

    var $has_many = array(
       'courtesy' => array(
          'class' => 'post',
          'other_field' => 'courtesy'
       ),
       'source' => array(
          'class' => 'post',
          'other_field' => 'source'
       )
    );

    And in my view, in the form I have called two single select drop down menus “source_id” and “courtesy_id”

    I just cant get it to work, and when it actually outputs something at all, it is this:

    A Database Error Occurred
    Error Number: 1146

    Table ‘blog.posts_sources’ doesn’t exist

    SELECT `sources`.* FROM (`sources`) LEFT OUTER JOIN `posts_sources` post_posts_sources ON `sources`.`id` = `post_posts_sources`.`_id` WHERE `post_posts_sources`.`_id` = 4

    Which seem wrong to me, because its a 1:M relationship, with the foreign keys in the table, not in a join table.

    Any idea what im doing wrong?
    Regards,
    modano

  • #814 / Jan 09, 2011 7:50pm

    WanWizard

    4475 posts

    You’ve defined both sides of the relationship the same way, that’s not going to work. The definition should be mirrored, like it is explained in the manual: http://datamapper.wanwizard.eu/pages/advancedrelations.html

    In the post model, try

    var $has_one = array(
       'courtesy' => array(
          'class' => 'source',
          'other_field' => 'courtesy_post'
       ),
       'source' => array(
          'class' => 'source',
          'other_field' => 'source_post'
       )
    );

    In the source model, try

    var $has_many = array(
       'courtesy_post' => array(
          'class' => 'post',
          'other_field' => 'courtesy'
       ),
       'source_post' => array(
          'class' => 'post',
          'other_field' => 'source'
       )
    );
  • #815 / Jan 09, 2011 8:05pm

    modano

    32 posts

    thanks for your reply WanWizard.
    It works when i try to save, but not when i try to get the relationships for output. still same error.

    Here is a code snippet:

    $post=new Post();
    $post->get_where(array('id'=>$variable_holding_id));
    $source=new Source();
    $post->where_related($source)->get();

    Grateful for any help
    modano

  • #816 / Jan 09, 2011 8:18pm

    WanWizard

    4475 posts

    You’re trying to get something related on an empty object? Assuming you want the source records, you should have reversed your question.

    I’m not sure (actually, I don’t think so) that you can get both source objects in one go. When using relationships, you have to specify (in case multple exist), which relation you want to query.

    Maybe you should explain what it is that you want to get out of this query?

  • #817 / Jan 09, 2011 8:34pm

    modano

    32 posts

    sorry if im confusing. just took the dog for a walk and thought about this and it hit me, the code i just posted where i try to retrieve data im not specifying what! it is i want, source or courtesy.
    is that the problem?

    the object is not empty, as i said, saving works fine so there is a post and there is sources.

  • #818 / Jan 09, 2011 8:48pm

    modano

    32 posts

    need to head to bed, will give it another go in the morning. thanks again for your help!
    ( “where_related” is where my problem lies)

  • #819 / Jan 10, 2011 4:10am

    WanWizard

    4475 posts

    In your example your source object is empty, so there’s nothing to relate to.

    If you want the related objects, you should not use the where_related(), because that generates a where clause on the related object (and that needs a valid source (object).

    You need

    // get the post record
    $post=new Post( $variable_holding_id );
    
    // get the related records
    $post->source->get();
    $post->courtesy->get();
  • #820 / Jan 10, 2011 7:26am

    modano

    32 posts

    i had been starring at the code far too long.
    thanks WanWizard, i got it working. Your help and some good night sleep was what was needed 😊

  • #821 / Jan 11, 2011 11:37am

    PoetaWD

    97 posts

    Hello,

    I was going over the LANGUAGE file to translate the error messages to PT-BR.

    I saw some lines that really took my attention:

    $lang[‘related_required’]  $lang[‘related_min_size’]  $lang[‘related_max_size’] $lang[‘dm_save_rel_failer’]  $lang[‘dm_save_rel_nothis’]  $lang[‘dm_save_rel_noobj’]

    How can I make a relationship required ?

    How can I access that error ? 

    What is “related_min_size” and ‘related_max_size’ for ?

    Are the answers to thos questions in the docs ?

    Btw,

    The PT-BR translation that is included in DM is all wrong. Here is a better one:

    <?php
    
    $lang['alpha_dash_dot']        = 'O campo %s deve conter apenas caractéres alfa-numéricos.';
    $lang['alpha_slash_dot']    = 'O campo %s deve conter apenas caractŽres alfa-numéricos.';
    $lang['min_date']        = 'O campo %s contém uma data inválida.';
    $lang['max_date']        = 'O campo %s contém uma data inválida.';
    $lang['min_size']        = 'O campo %s deve conter no minímo %s caractéres';
    $lang['max_size']        = 'O campo %s não pode exceder %s caractéres';
    $lang['transaction']    = 'O %s falhou para %s';
    $lang['unique']            = 'Já existe um registro de igual valor para o campo %s que deve ser único.';
    $lang['unique_pair']    = 'A combinação %s e %s já está sendo utilizada.';
    $lang['valid_date']        = 'O campo %s deve conter uma data válida.';
    $lang['valid_date_group']    = 'Os campos %s$s devem conter datas válidas';
    $lang['valid_match']        = 'O campo %s deve conter apenas %s';
    
    $lang['related_required']    = 'A relação entre os campos %s é necessária.';
    $lang['related_min_size']    = 'A relação %s deve ser no minímo %s.';
    $lang['related_max_size']    = 'A relação %s não pode exceder %s.';
    
    $lang['dm_save_rel_failer']    = 'O relacionamento %s não está definido corretamente.';
    $lang['dm_save_rel_nothis']    = 'Não foi possível salvar o relacionamento %s: O objeto não está salvo.';
    $lang['dm_save_rel_noobj']    = 'Não foi possível salvar o relacionamento %s: O objeto relacionado não foi salvo.';
    
    /* End of file datamapper_lang.php */
    /* Location: ./application/language/english/datamapper_lang.php */
  • #822 / Jan 11, 2011 1:10pm

    WanWizard

    4475 posts

    It is part of the ‘required’ validation check.

    See http://datamapper.wanwizard.eu/pages/validation.html#Related.Rules.

  • #823 / Jan 11, 2011 5:27pm

    paulipv

    26 posts

    I have a noob question regarding this tool.
    How can i get last inserted id ?

    Any docs for helper functions?

  • #824 / Jan 11, 2011 6:04pm

    WanWizard

    4475 posts

    After you have saved the object, the insert id (if supported by the database driver used) will be assigned to the id property.

    $user = new User();
    $user->name = 'John';
    $user->save();
    echo "John was saved with id ", $user->id;
  • #825 / Jan 11, 2011 6:57pm

    paulipv

    26 posts

    Thank you for your quick reply.

    While reading docs i stopped at languages => method localize_by_model

    Can you explain me what this method do?

      Update:

      Q1: A language file is loaded only if $model property is defined?

    function __toString()
        {
            return empty($this->name) ? $this->localize_label('newuser') : $this->name;
        }

    I guess that method replace the language “name” key if empty but my question is, why is $this->name if in lang config file key is defined like “model_key”.

    That looks my language config file:

    $lang['users_name']       = 'Name';
    $lang['users_email']      = 'Email';
    $lang['users_site_url']   = 'Url';

    Model name: users

    Source:

    class Users extends DataMapper {
    
        var $table = 'table1';
        var $model = 'users';
    
        public function getUsers() {
    
        }

    So i guess i call $this->users_name instead of $this->name , correct?

     

    Thank you
    paul

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

ExpressionEngine News!

#eecms, #events, #releases