How to put a value into a Pointer column? - php

I'm working with 'Parse.com' and I would like to store a value into a column in a Class that points to another Class.
For example:
$user = new ParseUser();
$user->set("username", "Batman");
$user->set("password", "darkknight");
$user->set("vehicle_ID", "UgTuNHEQEZ"); //pointer
Here, "vehicle_ID" is the name of the column. It's a pointer to the column id in the Class "Vehicle".
The error I get is that Parse expected the name of the Class I'm referring to. So, if I change the last line and do:
$user->set("Vehicle", "UgTuNHEQEZ");
The new user is created BUT the "vehicle_ID" is empty. Why is vehicle_ID not populated?

Double handicapped by not knowing php and discovering to my dismay that the parse.com API reference is a link to the github source, I did notice in the source a constructor function as follows:
public function __construct($className = null, $objectId = null,
$isPointer = false
) {
...
This suggests:
$vehicle = new ParseObject("OtherClass", "UgTuNHEQEZ", true);
$user->set("vehicle_ID", $vehicle);

Related

Concentrate text not function properly (Yii2 framework)

I want to concentrate some text from another table and put it in a new field.
$model1 = new CourseDetails();
$model1->course_shortform= CourseDetails::find()->select('course_shortform')->where(['course_name'=>$model->course_name]);
And then using the code below I was able to get the value from another table to fill in the field like so:
$model->intake_no = $model1->course_shortform;
Its all going well until i wanted to add some text for the intake no column:
$model->intake_no = $model1->course_shortform . "This is new text" ;
The system didn't display errors but it will show the text like this:
I wonder if got some alternative method to concentrate the text but I can't figure it out. I will glad if someone can help.
You are creating new instance of CourseDetails model then you create a query (instance of ActiveQuery) for selecting course_shortform and assigning that query into course_shortform property in that code of yours.
Instead of that you should skipt this line:
$model1 = new CourseDetails();
And you should use your query to find one model like this:
$model1 = CourseDetails::find()
->select('course_shortform')
->where(['course_name'=>$model->course_name])
->one();
Or if you need to create instance for $model1 by yourself you should use scalar() method to get the value returned by select directly.
$model1 = new CourseDetails();
$model1->course_shortform = CourseDetails::find()
->select('course_shortform')
->where(['course_name'=>$model->course_name])
->scalar();
You miss the fecth function .. for a model should be ->one()
$model1 = new CourseDetails();
$model1= CourseDetails::find()
->select('course_shortform')
->where(['course_name'=>$model->course_name])
->one();
and you should not assign the model to text field var so you don't $model1->course_shortform but on $model1

How to edit a property of another class and return it to another class?

So my problem is : I'm creating a management system with php, html5, css etc(school project).
When a user logs in I save its data in a class. The whole purpose of saving them is to later on use them when a purchase is done, so I can save the product ID and the user ID. But whenever I do the query to the DB I get an Undefined variable error.
This is my class and its methods in it. One is used to save data and one to return them.
class profile_attributes{
public $u_data;
function attributes($u_data){
$this->u_data=$u_data;
}
function attr_get(){
return $u_data;
}
}
How I initially send arguments
$u_data = mysqli_fetch_assoc($result);
$save_info = new profile_attributes()->attributes($u_data);
How I try to get them
$profile = new profile_attributes();
$loged_user = $profile->attr_get();
$user_id = $loged_user['id'];
It doesn't look like you're ever defining $u_data. All you're doing is creating a new instance of profile_attributes and then trying to pull the undefined $u_data.
You are creating two separate instances of your profile_attributes class. An instance's properties and data are specific to itself, and do not get shared if you create a second instance using the new class syntax.
Here's an example of what you're doing. Notice that the data inside the instances are not the same:
class Foo
{
public $data;
}
$instance1 = new Foo();
$instance1->data = array[1, 2, 3];
$instance2 = new Foo();
var_dump($instance1, $instance2);
You need to share the first instance you create and save the database result to the place where you want to retrieve those attributes.
$u_data is not defined, you must precede with $this
class profile_attributes{
public $u_data;
function attributes($u_data){
$this->u_data=$u_data;
}
function attr_get(){
return $this->u_data;
}
}
You were creating an instance to set the attributes and another to access them, use just one for both:
// Use the same instance to set and get atributes
$profile = new profile_attributes();
$u_data = mysqli_fetch_assoc($result);
$save_info = $profile->attributes($u_data);
$loged_user = $profile->attr_get();
$user_id = $loged_user['id'];

DBTable creates new row instead off updating the existing

I try to update an DB entry with now data, but i am just creating an new entry:
$client =$this->clientTable->find($id);
$client->CompanyName = $request->getPost('CompanyName');
$this->clientTable->update();
$this->_redirect('client/index');
Zend_Db_Table_Abstract::find() method returns Zend_Db_Table_Rowset object. You should use method which will return you Zend_Db_Table_Row object and use it.
For example:
$clientRow = $this->clientTable->fetchRow(array('id' => $id));
$clientRow->CompanyName = $request->getPost('CompanyName');
$clientRow->save();
If your's table primary key name is not 'id', change it to suitable value in the first line of code above.

How to make a "find and update" query in Paris

I am using Paris with Idiorm and I am having problems finding in the documentation a clear instruction on how to find and update a table.
I don't want to insert a sql query into the script. Is there any other way?
Paris is an Active Record implementation based on Idiorm.
Idiorm is an object-relational mapper and fluent query builder.
I am interested in doing something like count = count + 1 all in one go
I found this on their github site:
Updating records
To update the database, change one or more of the properties of the object, then call the save method to commit the changes to the database. Again, you can change the values of the object's properties either by using the set method or by setting the value of the property directly:
$person = ORM::for_table('person')->find_one(5);
// The following two forms are equivalent
$person->set('name', 'Bob Smith');
$person->age = 20;
// Syncronise the object with the database
$person->save();
Creating new records
To add a new record, you need to first create an "empty" object instance. You then set values on the object as normal, and save it.
$person = ORM::for_table('person')->create();
$person->name = 'Joe Bloggs';
$person->age = 40;
$person->save();
After the object has been saved, you can call its id() method to find the autogenerated primary key value that the database assigned to it.
Checking whether a property has been modified
To check whether a property has been changed since the object was created (or last saved), call the is_dirty method:
$name_has_changed = $person->is_dirty('name'); // Returns true or false
According to the documentation on the github page, in idiorm you can update a record by doing the following:
$person = ORM::for_table('person')->find_one(5);
// The following two forms are equivalent
$person->set('name', 'Bob Smith');
$person->age = 20;
// Syncronise the object with the database
$person->save();
Or to do it in 'paris' you do:
$user = Model::factory('User')->find_one($id);
$user->name = 'Paris';
$user->save();

Codeigniter how to map a class sent to a model

I have a SectorModel with this function:
public function update(Sector $sector) {
$this->db->where('sector_id', $sector->getScetor_id());
return $this->db->update(_SECTOR_, $sector);
}
There are times that I’ll change only the name of the Sector object:
$Sector = new Sector();
$Sector->setSector_name = 'test';
$this->SectorModel->update($Sector);
The generated select looks like:
UPDATE realestate_sector SET sector_name = 'teste', sector_description = NULL
It will update but will set all other properties to NULL because it was not set on my object.
Right now, I have to fill the whole object before sending it.
Is there a way to map the Sector class and update only what was sent on the object?
Thanks in advance for any help.
Sorry for any typos, my English is not good =)
Just loop through all your object's properites and then if any is NULL just drop it with unset.
Here is your model's method edited to achieve that:
public function update(Sector $sector)
{
foreach($sector as $k=>$v)
{
if($v === NULL)
unset($sector->$k)
}
$this->db->where('sector_id', $sector->getScetor_id());
return $this->db->update(_SECTOR_, $sector);
}
Here you can find some info about iterating objects in PHP
The easiest to do this would be to rather use a array - docs here http://codeigniter.com/user_guide/database/active_record.html#update - you just create a array of all the columns with their values that you want to update and perform a $this->db->update('mytable', array('name' => 'test'), array('id' => $id)); call. This will only update the columns you specified in the First array. With the second array acting as your WHERE expression.
The only reason I can think of as to why your other values are being set to NULL is because in your example you create a new instance of the class and the other values must either have been set to nothing or are set to NULL. It would (If this is the case) be better to get a record from the table and then change and values on the populated record and pass that to the function to update.
Hope that helps.

Categories