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]
  • #916 / Jun 20, 2009 10:28pm

    OverZealous

    1030 posts

    @PoetaWD
    Try:

    if( $u->exists() ) {
        // $u exists
    } else {
        // $u does not exist
    }

    Also, you can use this to look up a user by id (it’s just shorthand for what you already wrote):

    $u->get_by_id($userid);

    Finally, make sure you look through the DataMapper documentation, at http://stensi.com/datamapper/index.html

    Good Luck, and if you have any questions, feel free to ask.

  • #917 / Jun 21, 2009 9:26am

    PoetaWD

    97 posts

    Hey !

    Thank you for the quick reply ... I was giving a second look at the user guide and I foud that he used the empty() function…

    So..

    I did it like this:

    if(empty($u->profile->id))

    and… it worked !

    I will also try the way you said for learning porpose…  thank you very much

  • #918 / Jun 21, 2009 1:27pm

    OverZealous

    1030 posts

    @PoetaWD:
    Just an FYI:

    if($u->profile->exists())

    does the exact same thing as your code.  What I like about that is it is a little more obvious what you are trying to check (that profile exists, rather than profile->id is not zero).

  • #919 / Jun 23, 2009 3:48pm

    gshipley

    6 posts

    Thanks for the great tool!  Quick question about select_max that I don’t seem to understand.

    $tmpUser = new User ( );
    $tmpUser->where ( ‘username’, ‘someusername’);
         
    $tmpUser->get ();
         
    $movieCount = $tmpUser->usermovie->get()->count();
    echo “Count is “.$movieCount;

    ^^ works great.  Now I want to print the last watched movie:

    $tmpUser->usermovie->select_max(‘last_watched’)->get();
    echo “
    ..”.$tmpUser->usermovie->last_watched.”
    “;

    prints the last watched time.  From the manual, select_max only returns an object with the last_watched field populated (in this example).  How do I then relate this back to the object with all fields?

    for example:
    echo “
    ..”.$tmpUser->usermovie->movie->get().”
    “;

  • #920 / Jun 23, 2009 4:14pm

    OverZealous

    1030 posts

    @gshipley

    This is how you get what you want:

    // generates a query like (excluding the joins):
    // SELECT * FROM usermovies ORDER_BY last_watched LIMIT 1
    $tmpUser->usermovie->order_by('last_watched', 'DESC')->get(1);

    Now $tmpUser->usermovie will contain a single usermovie with the maximum last_watched value.

    Understand this, however: As far as the database knows, there could be more than one movie with the same maximum “last_watched” (apparently, you are using dates here, so it should not happen).  That’s why select_max cannot return a single usermovie.  The example I give above only works consistently for columns where there can only be one max value in a column.

  • #921 / Jun 23, 2009 4:26pm

    gshipley

    6 posts

    @gshipley

    This is how you get what you want:

    // generates a query like (excluding the joins):
    // SELECT * FROM usermovies ORDER_BY last_watched LIMIT 1
    $tmpUser->usermovie->order_by('last_watched', 'DESC')->get(1);

    Now $tmpUser->usermovie will contain a single usermovie with the maximum last_watched value.

    Understand this, however: As far as the database knows, there could be more than one movie with the same maximum “last_watched” (apparently, you are using dates here, so it should not happen).  That’s why select_max cannot return a single usermovie.  The example I give above only works consistently for columns where there can only be one max value in a column.

    Perfect, thanks!

  • #922 / Jun 24, 2009 5:11pm

    PoetaWD

    97 posts

    Can you help me with this error ?

    Fatal error: Class 'tiposvendum' not found in C:\xampp\htdocs\ecos\system\application\libraries\datamapper.php on line 3005

    Model: tiposvenda.php

    <?php
    class Tiposvenda extends DataMapper {
        
        function Tiposvenda()
        {
            parent::DataMapper();
        }
    }
    ?>

    Database:

    CREATE TABLE IF NOT EXISTS `tiposvendas` (
      `id` bigint(20) NOT NULL AUTO_INCREMENT,
      `stNome` varchar(100) NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

    Controller:

    $obj = new Tiposvenda();
                $obj->get();
                
                $data['objects'] = $obj;
                
    
                        
                $this->load->view('tipo_venda/lista', $data);

    Also, what is the better way of sending the result of all itens to the view file ?

    Like, I want to build a html table with all the objets of the type ‘Tipovenda’...


    Thank you

  • #923 / Jun 24, 2009 5:23pm

    PoetaWD

    97 posts

    Just found that the error was because the helper was changing the names to plural… but the second question stays…

    I did like this:

    $obj = new Tiposvenda();
                $obj->get();
                
                foreach ($obj->all as $objt)
                {
                    $data['objetos'][$obj->id]['stNome'] = $obj->stNome;
                }
                        
                $this->load->view('tipo_venda/lista', $data);

    But is there a better way ? Because I will have to do a second loop in the view file… and might be a better, quiker way to do it…

    Thank you !

  • #924 / Jun 24, 2009 5:26pm

    tdktank59

    322 posts

    That’s basically the fastest way that I know…

    You could do $data[‘objects’] = $obj->all; but that doesn’t do much since you have to do it anyways.
    Otherwise that’s the best you can do… (To my knowledge)

  • #925 / Jun 24, 2009 5:37pm

    PoetaWD

    97 posts

    That’s basically the fastest way that I know…

    You could do $data[‘objects’] = $obj->all; but that doesn’t do much since you have to do it anyways.
    Otherwise that’s the best you can do… (To my knowledge)

    Just updated my post…

    But… how to write the information in the view file ? How would the loop be ?

    Like this ?

    foreach ($object as $obj)
    {
        echo $obj->title
    }

    I am sorry if those are newby questions… I am really trying to learn php.. hehe

    Thank you

  • #926 / Jun 24, 2009 5:42pm

    tdktank59

    322 posts

    Ah lets see if I can help with this!

    First off in the controller

    $obj = new Tiposvenda();
    $obj->get();
    
    $data['objetos'] = $obj->all; // or $obj in the view you can add the ->all     
                        
    $this->load->view('tipo_venda/lista', $data);

    Now in the view

    foreach ( $objetos as $o ) // or $objetos->all if you passed $obj in the controller
    {
        echo $o->stNome.'
    ';
    }

    That would print out

    $stNome
    $stNome
    $stNome
    $stNome

    where $stNome is the value from the database for each row.

    Hope that makes sense

  • #927 / Jun 24, 2009 5:59pm

    PoetaWD

    97 posts

    Ah lets see if I can help with this!

    First off in the controller

    $obj = new Tiposvenda();
    $obj->get();
    
    $data['objetos'] = $obj->all; // or $obj in the view you can add the ->all     
                        
    $this->load->view('tipo_venda/lista', $data);

    Now in the view

    foreach ( $objetos as $o ) // or $objetos->all if you passed $obj in the controller
    {
        echo $o->stNome.'
    ';
    }

    That would print out

    $stNome
    $stNome
    $stNome
    $stNome

    where $stNome is the value from the database for each row.

    Hope that makes sense

    It sure does !

    Thank you !

    If this is the best way of doing… i will do it…

    Thank you very much !

  • #928 / Jun 24, 2009 6:08pm

    tdktank59

    322 posts

    Not sure if its the best way but its the best way I know how to do it lol…
    And the only way I know how to…

    Of course there are a few other ways like taking what you want and processing it into anther array before passing to the view… But at some point its why?

  • #929 / Jun 24, 2009 6:14pm

    OverZealous

    1030 posts

    Just to throw in my $0.02:

    I think that tdktank59’s example is the best way to handle it.  It has the least amount of duplication (copying the data into another array would eat up CPU and RAM, with no benefit), and makes the most sense MVC-wise: the model is the $objetos, which is what is passed between the View and the Controller.

    Personally, I waffle between passing in $object and $object->all.  Both make sense for different reasons.  The nice thing about passing in $object->all directly, is you might reduce the risk of accidentally attempting to loop over $object (which I have done many, many times).

  • #930 / Jun 24, 2009 6:17pm

    tdktank59

    322 posts

    And looping $object is quite large lol..

    Only thing id say is keep it consistent throughout your site so you don’t confuse yourself.
    I like using the ->all in the controller since then im only passing the data. And can use it right away. Unless of course its a larger array containing multiple rows of data in which case I loop it..

    do this to see what looping $object can do lol…

    echo '<pre>'.print_r($objetos).'</pre><p>‘;
    </pre>
    its quite large lol…

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

ExpressionEngine News!

#eecms, #events, #releases