Concentrate text not function properly (Yii2 framework) - php

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

Related

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'];

Laravel 5.2 updated table now model wont work

I updated my notes table with 4 new columns: classification, pod, sampled, followup.
I would update my model like so (which still works like this):
$note->account_id = $account->id;
$note->name = $request->input('account-note-title');
$note->description = $request->input('account-note-description');
$note->save();
But if I try and post to the new columns it fails.
$note->account_id = $account->id;
$note->name = $request->input('account-note-title');
$note->description = $request->input('account-note-description');
$note->classification = $request->input('account-note-classification');
$note->pod = $request->input('account-note-pod');
$note->sampled = $request->input('account-note-samplebrand');
$note->followup = $request->input('account-note-followup-date');
$note->save();
Do I have to refresh the model or anything?
You can create new value using laravel Create methode like below
ModelName::create($request->all())
In this case you have to put your all input field in the $fillable variable of MODEL.
You can update item using the following method
$model = findOrFail($id)
$model->update($request->all())

How to put a value into a Pointer column?

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);

How to edit Google Calender event using Zend_Gdata_Calendar class ?

When you retrieve data from a calendar you can set the user before the data is retrieved like this
$gdataCal = new Zend_Gdata_Calendar($this->client, $this->domain);
$calendar_list = $gdataCal->getCalendarListFeed();
$query = $gdataCal->newEventQuery();
$query->setUser( $username . '%40domain.com');
But how can you do this when you want to create/update an event?
$service = new Zend_Gdata_Calendar($this->client, $this->domain);
$event = $service->newEventEntry();
There are no methods like setUser on any of the objects returned
I have been into similar condition but, the wordaround that I did was :
$service->delete($event->getEditLink()-><URL-Of-The-Event>);
and later, create another one.
However, that was approximately an year ago. I am not sure if any new functions have come up because, I never needed it after that.
If no one answers, this wordaround has to work for you :)

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();

Categories