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]
  • #406 / Jan 20, 2009 3:38pm

    tdktank59

    322 posts

    UPDATE: Found the problem… The controllers name was the same as the models… Problem fixed now.

    Im having an issue with the get function

    Error Message:

    [20-Jan-2009 14:35:03] PHP Fatal error:  Call to undefined method Brainstorm::get() in /home/fourtwo1/public_html/dirt/application/controllers/brainstorm.php on line 18

    Controller

    class Brainstorm extends Controller {
    
        function Brainstorm()
        {
            parent::Controller();
        }
    
        function index()
        {
    
        }
    
        function grid()
        {
            $b = New Brainstorm();
            $b->get();
            //$b->user->get();
            //$b->data_source->get();
            //$b->issue_type->get();
            //$b->status->get();
           // $b->priority->get();
    
            foreach ($b->all as $bstorm)
            {
                echo pre_print_r($bstorm);
            }
    
        }
    }
    
    /* End of file brainstorm.php */
    /* Location: ./system/application/controllers/brainstorm.php */

    Model

    <?php
    
    /**
     * Brainstorm Class
     *
     * Transforms users table into an object.
     * This is just here for use with the example in the Controllers.
     *
     * @licence     MIT Licence
     * @category    Models
     * @author      Simon Stenhouse
     * @link        <a href="http://stensi.com">http://stensi.com</a>
     */
    class Brainstorm extends DataMapper
    {
        var $table = 'brainstorms';
    
        //var $has_one = array("user", "status", "priority", "issue_type");
        //var $has_many = array("data_source");
    
        var $validation = array(
            array(
                'field' => 'title',
                'label' => 'Title',
                'rules' => array('trim', 'unique', 'min_length' => 2, 'max_length' => 255, 'alpha_dash')
            ),
            array(
                'field' => 'description',
                'label' => 'Description',
                'rules' => array('trim', 'alpha_dash')
            ),
            array(
                'field' => 'orgin_date',
                'label' => 'Orgin Date',
                'rules' => array('required', 'trim', 'is_natural')
            ),
            array(
                'field' => 'estimated_count_total',
                'label' => 'Estimated Count Total',
                'rules' => array('trim', 'is_natural')
            ),
            array(
                'field' => 'estimated_count_total',
                'label' => 'Estimated Count Manual',
                'rules' => array('trim', 'is_natural')
            )
        );
    
        /**
         * Constructor
         *
         * Initialize DataMapper.
         */
        function Brainstorm()
        {
            parent::DataMapper();
        }
    
        // --------------------------------------------------------------------
    
    }
    
    /* End of file brainstorm.php */
    /* Location: ./application/models/brainstorm.php */

    datamapper is being autoloaded

  • #407 / Jan 22, 2009 12:57am

    expo

    1 posts

    hi all.

    just playing around with the datamapper today and revealing how powerful it is. it will be a great tool to further increase the speed of developing apps.

    I wanted to create a helper to determine the current position within the returned array in a foreach loop.

    I can return the total array elements fine “count(Object->all)” but I can’t seem to get hold of the numeric key for each array.

    Has anyone done this before and could perhaps provide some pointers.

    thanks.

  • #408 / Jan 22, 2009 5:41pm

    ntheorist

    84 posts

    @expo

    have you tried $keys = array_keys($obj->all); ?

    you can also access the keys with a foreach

    foreach($obj->all as $key => $row){
         echo $key,' : ',$row->name,br();
    }

    CC

  • #409 / Jan 22, 2009 6:38pm

    wolffc

    14 posts

    Is it possible to give an order column to the joining table?  Lets say a user can pick 3 of 10 options and set a specific order.  Would they be inserted and retrived in the same order?


    Thanks

  • #410 / Jan 22, 2009 7:26pm

    tdktank59

    322 posts

    So im having a problem saving a relationship…

    For some reason no error shows up even tho it is failing… and on top of that its inserting the brainstorm but not the user relation…

    I have removed the forms and stuff its just the datamapper stuff, Everything is being passed properly too

    So let me know if you have any ideas…

    Also all variables defined that are not show do exist…

    Controller Code

    $b = New Brainstorm();
    $b->title                     = set_value('title');
    $b->description               = set_value('description');
    $b->orgin_date                = mktime(0,0,0,$date[0],$date[1],$date[2]);;
    $b->estimated_count_total     = set_value('estimated_count_total');
    $b->estimated_count_manual    = set_value('estimated_count_manual');
    $u = New User();
    $u->where('id',set_value('created_by'))->get();
    if ($b->save($u))
    {
    $data['success'] = 'Brainstorm Created';
    }
    else
    {
    $data['error'] = $b->error->string;
    }

    brainstorm model

    var $table = 'brainstorms';
    
    var $has_one = array("user", "status", "priority", "issue_type");
    var $has_many = array("data_source");
    
    var $validation = array(
    array(
    'field' => 'title',
    'label' => 'Title',
    'rules' => array('trim', 'unique', 'min_length' => 2, 'max_length' => 255)
    ),
    array(
    'field' => 'description',
    'label' => 'Description',
    'rules' => array('trim')
    ),
    array(
    'field' => 'orgin_date',
    'label' => 'Orgin Date',
    'rules' => array('required', 'trim', 'is_natural')
    ),
    array(
    'field' => 'estimated_count_total',
    'label' => 'Estimated Count Total',
    'rules' => array('trim', 'is_natural')
    ),
    array(
    'field' => 'estimated_count_manual',
    'label' => 'Estimated Count Manual',
    'rules' => array('trim', 'is_natural')
    ),
    array(
    'field' => 'user',
    'label' => 'User',
    'rules' => array('trim', 'required')
    )
    );

    User Model

    var $has_many = array("brainstorm");
    
    var $validation = array(
    array(
    'field' => 'username',
    'label' => 'Username',
    'rules' => array('trim', 'unique', 'strtolower', 'min_length' => 2, 'max_length' => 20)
    ),
    array(
    'field' => 'password',
    'label' => 'Password',
    'rules' => array('required', 'trim', 'min_length' => 3, 'max_length' => 40, 'encrypt')
    ),
    array(
    'field' => 'email',
    'label' => 'Email Address',
    'rules' => array('required', 'trim', 'strtolower', 'unique', 'valid_email')
    ),
    array(
    'field' => 'first_name',
    'label' => 'First Name',
    'rules' => array('required', 'trim')
    ),
    array(
    'field' => 'last_name',
    'label' => 'Last Name',
    'rules' => array('required', 'trim')
    )
    );
  • #411 / Jan 22, 2009 7:29pm

    ntheorist

    84 posts

    Is it possible to give an order column to the joining table?  Lets say a user can pick 3 of 10 options and set a specific order.  Would they be inserted and retrived in the same order?


    Thanks

    I’ve been toying with this idea of building in methods that access additional fields in the relationship tables..

    essentially though, you can add whatever fields you like, in your linking table as long as you have the id, model1_id, model2_id fields..

    so say you want to get a users groups, which would have a sort order, you could add in an order_number column on the groups_users table, and then (with a join specified), you can try

    $user = new User();
    $user->where('id',$userID)->get();
    $user->group->order_by('groups_users.order_number', 'asc');
    $user->group->get();

    does that make sense?.. you just have to literally specify the relationship table name for now.

    CC

  • #412 / Jan 23, 2009 6:11am

    robertcsmith

    9 posts

    @tdktank59
    typically when saving new or updated Brainstorm objects and relations you should load the temp object values via $this->input->post(’{fieldname}’). The forms fieldnames dont even need to be the extact same name as the db field since you pass its value into the object property and then upon a save attempt, validation runs off that, and either returns errors or has a successful save. 

    The set_value method is defined as “
    set_value()

    Permits you to set the value of an input form or textarea. You must supply the field name via the first parameter of the function. The second (optional) parameter allows you to set a default value for the form. Example:
    <input type=“text” name=“quantity” value=”<?php echo set_value(‘quantity’, ‘0’); ?>” size=“50” />

    The above form will show “0” when loaded for the first time.”

    I dont think that method would return anything until after validation at least.

    I was working on and is posted in our projects repository a couple of validation prep and preprocess validation rules including a method to track which user last updated the record.

  • #413 / Jan 23, 2009 6:15am

    The Hamburgler

    28 posts

    Hello again.
    I’ve been busy developing an extension to the Datamapper Model that will provide methods to quickly draw forms/tables of your Datamapper models. It’s going well, Datamapper has proved to be very effective.

    However a number of times I have met an issue removing relationships from a model.

    Example:
    I have two related models.

    class School extends DataMapper {
    
        var $has_many = array("student");
    
        function School()
        {
            parent::DataMapper();
        }
    }
    class Student extends DataMapper {
    
        var $has_one = array("school");
    
        function Student()
        {
            parent::DataMapper();
        }
    }

    I wish to remove a subset of pupils from a school and then display the remaining school pupils

    // ini models
    $sch = new School();
    $stu = new Student();
    
    // load school
    $sch->get_by_id(1);
    
    // load subset of students
    $stu->where_in('id', array(1, 3, 4, 7));
    $stu->get();
    
    // remove subset of students from school
    $sch->delete($stu->all);
    
    // refresh remaining students at school
    $sch->school->get(); // throws 'Call to a member function get() on a non-object' Error

    There are ways around this issue, you can re-initialise the student object within the school, but its not very pretty. Just wondering if anybody else has has this issue or can see a cleaner solution?

    Cheers.

  • #414 / Jan 23, 2009 6:52am

    robertcsmith

    9 posts

    um I think you meant to call $sch->student->get() in this:

    // refresh remaining students at school
    $sch->school->get(); // throws 'Call to a member function get() on a non-object' Error

    but if you want to use $stu as an object again you’ll need to first clear it and then instantiate it as a new object with $stu = new Student; $stu->get();

  • #415 / Jan 23, 2009 7:12am

    The Hamburgler

    28 posts

    um I think you meant to call $sch->student->get() in this:

    // refresh remaining students at school
    $sch->school->get(); // throws 'Call to a member function get() on a non-object' Error

    Oops, yes thats right.
    My current solution is too re instantiate the student object within the school model. i.e.

    // remove subset of students from school
    $sch->delete($stu->all);
    
    // instantiate schools students model
    $sch->student = new Student();
    
    // refresh remaining students at school
    $sch->student->get();

    I was just wondering why datamapper removes the related object from model instead of just removing the link record and refreshing the related object?

  • #416 / Jan 23, 2009 3:38pm

    tdktank59

    322 posts

    @tdktank59
    typically when saving new or updated Brainstorm objects and relations you should load the temp object values via $this->input->post(’{fieldname}’). The forms fieldnames dont even need to be the extact same name as the db field since you pass its value into the object property and then upon a save attempt, validation runs off that, and either returns errors or has a successful save. 

    The set_value method is defined as “
    set_value()

    Permits you to set the value of an input form or textarea. You must supply the field name via the first parameter of the function. The second (optional) parameter allows you to set a default value for the form. Example:
    <input type=“text” name=“quantity” value=”<?php echo set_value(‘quantity’, ‘0’); ?>” size=“50” />

    The above form will show “0” when loaded for the first time.”

    I dont think that method would return anything until after validation at least.

    I was working on and is posted in our projects repository a couple of validation prep and preprocess validation rules including a method to track which user last updated the record.

    This doesn’t answer my question Robert, And yes set_value() does work after the validation has run (Good thing it has by the time I’m creating the objects…).

    The problem im having is the fact that it will not save the relationships for some reason, ALl values ARE being passed and populated properly.

  • #417 / Jan 23, 2009 6:06pm

    OES

    127 posts

    Ok I think I have gone totally mad. I hope someone can help before I pull all my hair out.

    I have setup a new install and have installed as per the manual and have left all config items the same and have no table prefix as per the suggestion. (Can I take it config/datamapper is auto loaded ?).

    But I just cannot get datamapper to relate tables.

    Ie,  A very basic example User & Country. I have set the tables below with sample data excatly as you can see here. http://screencast.com/t/sUHM9ERWluG.

    Also set up the Models as per examples. Its all real basic.

    But If I run this code.

    $u = new User();
    $u->get();
            
    $u->country->get();

    Im just trying to get all users and the country.

    No matter whatever I try I just get SQL errors ie.

    Unknown column 'countries.*' in 'field list'
    SELECT `countries`.`*` FROM (`countries`) LEFT JOIN `countries_users` ON `countries`.`id` = `countries_users`.`country_id` LEFT JOIN `users` ON `users`.`id` = `countries_users`.`user_id` WHERE `users`.`id` = 1

    Why is it trying to select countries?.

    I also tried setting the auto_populates to TRUE and still no countrys.

    Hope someone can advise.

    And thank you in advance if you can.

  • #418 / Jan 23, 2009 6:23pm

    tdktank59

    322 posts

    @OES

    What are you trying to get???

    I think your problem is calling the country with $u…

    Checkout the examples.php in the saving relations area it might answer your question otherwise let me know what you are trying to get and ill see if I can help.


    Also an update to my problem…
    For some reason if I save the object before the relations then get the object back and then save the relations it seems to work…

    $b = New Brainstorm();
    $b->title                     = set_value('title');
    $b->description               = set_value('description');
    $b->orgin_date                = mktime(0,0,0,$date[0],$date[1],$date[2]);;
    $b->estimated_count_total     = set_value('estimated_count_total');
    $b->estimated_count_manual    = set_value('estimated_count_manual');
    $b->save();
    
    $b1 = New Brainstorm();
    $b1->where('title',set_value('title'))->get();
    
    $u = New User();
    $u->where('id',set_value('created_by'))->get();
    
    // Save the relations
    $b1->save(array($u));
  • #419 / Jan 23, 2009 6:34pm

    OES

    127 posts

    Thank you tdktank59

    I may be a thick head but I though the idea of ORM was to do all my inner joins etc.

    So im just trying to get all users with the country.

    So just imagine I want to look all usernames and the Country.

    Any suggestions ?

    And thank you

  • #420 / Jan 23, 2009 6:36pm

    tdktank59

    322 posts

    SO

    User 1 -> country
    user 2 -> country
    user 3 -> country

    or

    Country -> user1, user2, user3
    country2 -> user4, user5

    which method?

    or

    user1
    user2
    user3

    contry1
    country2
    country3

    Im still not sure what you are looking for…

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

ExpressionEngine News!

#eecms, #events, #releases