Re: Redbean Getting Foreign Values for Objects

60 views
Skip to first unread message

jelphi...@gmail.com

unread,
May 9, 2013, 9:51:43 PM5/9/13
to redbe...@googlegroups.com
Any help would be greatly appreciated!


Matthew Frederico

unread,
May 10, 2013, 12:11:52 AM5/10/13
to redbe...@googlegroups.com
Maybe this will help?

<?php
include('rb.php');

$dbfile     = 'testfile.sqlite';
$initialize = false;

if (!file_exists($dbfile)) {
    $initialize = true;
}

R::setup('sqlite:'.$dbfile,'user','pass');
// R::debug();

// Create and relate
if ($initialize) {
    // The chassis type of this car
    $type = R::dispense('type');
    $type->name = 'Sedan';
    R::store($type);

    // The make of this car
    $make = R::dispense('make');
    $make->name = 'Ford';
    $make->type = $type;
    R::store($make);

    // The plant where the car was made
    $plant   = R::dispense('plant');
    $plant->name = 'Detroit';
    $plant->make = $make;
    R::store($plant);

    print "Run this one more time .. \n";
}
// Lets see how we did
else {
    print "Cool eh?\n";
    $plant = R::load('plant',1); // Just a test
    print($plant->make->name."\n");
}



On Fri, May 3, 2013 at 1:34 AM, <jelphi...@gmail.com> wrote:
Hi Guys,

I've recently decided to give RedBeanPHP a try! 

Being a newbie to this I have been following the documentation from the RedBean website, however I am having troubles trying to figure out how to return an array of objects, whose values are those mapped from the foreign key tables..

I am using MySQL and I have the following tables and columns

Database Name = test

table 1 - plant
id
make_id
type_id
registration


table 2 - make
id
make


table 3 - type
id
type


[code]
<?php
    require 'rb.php';

    R::setup('mysql:host=localhost;dbname=test',
        'root',''); 

    //beans
    echo '<pre>';

$plantcount = R::count('plant'); //counts all pages

$id1 = 0;
$plant = R::batch('plant',array($id1,$plantcount));
        
        print_r($plant);

    R::close();

?>
[/code]

This prints an array of objects of type plant. I am seeking some help with my syntax so that the data not the primary key of the relationship is what is added as the property value of the objects..
i.e. plant[1].Make = "Ford" not plant[1].Make = 4.. 


Thank you for your help!

Regards
Jordan

--
You received this message because you are subscribed to the Google Groups "redbeanphp" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redbeanorm+...@googlegroups.com.
To post to this group, send email to redbe...@googlegroups.com.
Visit this group at http://groups.google.com/group/redbeanorm?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
--
-- Matthew Frederico
Message has been deleted

jelphi...@gmail.com

unread,
May 14, 2013, 5:54:40 PM5/14/13
to redbe...@googlegroups.com

Thanks for your reply Matt,

The code works great, and I've adapted it slightly to suit my application.

Currently I am trying to Load up an existing bean to update its values.

Just a quick definition: plant is a machine - not a factory. I just thought I would clear this up as it could potentially affect the object relationships..

Currently i'm doing the following: I'm handing in a class object $p into the UpdatePlantObject function. This object is the one which contains all the updated data. I then load up the corresponding object from the database $plant (via $plant   = R::load('plant',$p->PlantNumber); ) This is because the PlantNumber property of the $p object is the primary key of the plant in the database! Now the $plant object is loaded from the database I now wish to update its values so that they match the values of $p.

The problem I am having is that when I do this, new values are added to my costcentre, type, make & depot tables regardless if they are already existing in the table. I.e. the table is no longer showing unique values. 

How do I update the related values of my $plant object without adding new (duplicated) values to the related tables?

function UpdatePlantObject($p)
    {       
    
    require 'rb.php';

    R::setup('mysql:host=localhost;dbname=ausgridtest',
        'root',''); 


      //make
      $make = R::dispense('make'); 
      $make->name = $p->Make;  
      
      //type
      $type = R::dispense('type');
      $type->name = $p->Type;
      
      //depot
      $depot = R::dispense('depot');
      $depot->name = $p->Depot;

      //costcentre
      $costcentre = R::dispense('costcentre');
      $costcentre->number = $p->CostCentre;

      // ---------------------------
      $plant   = R::load('plant',$p->PlantNumber);
      $plant->make = $make;
 $plant->make = $make;
      $plant->costcentre = $costcentre;



      $plant = R::store($plant);

      //error_reporting(-1);
      printVar($plant);
    R::close();   //close the connection to the database

    }
Reply all
Reply to author
Forward
0 new messages