Symfony Unset Attribute - session

I have the following session data from symfony2
[attributes:protected] => Array
(
[_security_secured_area] =>
[ids] => Array
(
[0] => 1426
[1] => 1427
[2] => 1428
)
)
I want to remove the ids 1428. I have read about $session->remove but want to, how to use it for multi-d array ...

The session class is just a store for the data. So you have to handle this on your own:
$ids = $session->get('ids');
$ids = someFunctionToUnsetTheId($ids, $idToRemove);
$session->set('ids', $ids);
If you have some more information about the IDs, you could look into namespacing the session keys:
$tokens = $session->get('tokens');
$tokens['c'] = $value;
$session->set('tokens', $tokens);
$session->set('tokens/c', $value);
Code example taken from the Symfony doc.

Related

I need To All Data In $qryy It Return Only Last Record

How I Return all data of $qryy it's return me only last record. How I need return all off data.
public function get_subcategory(){
$qry = $this->db->get("shopping_category");
foreach($qry->result() as $row){
$this->db->where("c_id",$row->id);
$qryy = $this->db->get("shopping_subcategory");
}
return $qryy;
}
You can try this
public function get_subcategory() {
$result = [];
$qry = $this->db->get("shopping_category");
foreach ($qry->result() as $row) {
$this->db->where("c_id", $row->id);
$qryy = $this->db->get("shopping_subcategory");
$result[] = $qryy;
}
return $result;
}
The reason you get the last record is, every time you loop data through foreach it keeps replacing $qryy = $this->db->get("shopping_subcategory");
to fix this you can simply change $qryy into an array $qryy[] like this.
To improve your query. you can simply try
$qryy = [];
$this->db->select("shopping_subcategory.*");
$this->db->from("shopping_category");
$this->db->join('shopping_subcategory', 'shopping_category.id = shopping_subcategory.c_id');
$sql= $this->db->get();
$qryy = $sql->result_array();
$data['category'] = $qryy;
$this->load->view('view_name', $data);
In view
$category will show your data
I think it solve your problem. would you mind if you give it a try?
since you want from table shopping_category is only id i try to fetch it so it's not a heavy duty on your server.
$result = [];
$qry = $this->db->select("id")->get('shopping_category');
foreach ($qry->result() as $row) {
$this->db->where("c_id", $row->id);
$qryy = $this->db->get("shopping_subcategory")->result();
$result[] = $qryy;
}
return $result;
Hope that helps :)
you should use another approach to resolve your problem: use joins to query the database only once:
$this->db->join("shopping_subcategory t2","t2.c_id=t1.id");
$qry = $this->db->get("shopping_category t1");
return $qry->result();
if you output above with print_r($qry->result()), you get similar to below:
Array
(
[0] => stdClass Object
(
[ID] => 1
[company_ID] => 1
[name] => aaa
)
[1] => stdClass Object
(
[ID] => 2
[company_ID] => 1
[name] => bbb
)
[2] => stdClass Object
(
[ID] => 4
[company_ID] => 2
[name] => ccc
)
)
to resolve your approach: you need to make $qryy an array, to store each subquery data, right now you are overwriting the variable $qryy with each loop and therefore only get the last result:
$qry = $this->db->get("shopping_category");
$qryy=array();
foreach($qry->result() as $i=>$row){
$this->db->where("c_id",$row->id);
$qryy[$i] = $this->db->get("shopping_subcategory")->result();
}
return $qryy;
if you output above with print_r($qryy), you get similar to below:
Array
(
[0] => Array
(
[0] => stdClass Object
(
[ID] => 1
[company_ID] => 1
[name] => aaa
)
[1] => stdClass Object
(
[ID] => 2
[company_ID] => 1
[name] => bbb
)
)
[1] => Array
(
[0] => stdClass Object
(
[ID] => 4
[company_ID] => 2
[name] => ccc
)
)
)
therefore, depending which approach you use, you'll need to take care of your data output differently.
helpful info on joins here

In eloquent fetching data in fieldname=>fieldvalue keys

In laravel 5.8 making request
$searchResultsArray = SearchResult
::getBySourceId($next_sourceId['source_id'])
->select( 'field', 'value' )
->get()
->toArray();
I got resulting rows like :
$searchResultsArray::Array
(
[0] => Array
(
[field] => Id
[value] => 302530
)
[1] => Array
(
[field] => Domain
[value] => site.com
)
If there is a way to get
array of fieldname=>fieldvalue keys, like
Id => 302530
Domain=> site.com
without additive foreach block in my code?
If yes how?
Yes there is. Eloquent always returns collections. You could modify the collection first with mapWithKeys() (https://laravel.com/docs/5.8/collections#method-mapwithkeys) and then convert it to an array.
Try something like this:
$searchResultsArray = SearchResult
::getBySourceId($next_sourceId['source_id'])
->select( 'field', 'value' )
->get()
->mapWithKeys(function ($item) {
return [$item['field'] => $item['value']];
})
->all();

How to merge the return from one to one relationship

I am using one to one relationship in laravel.
I have two models one is User and another is UserDetails.
The relationship is in User model
public function userDetails(){
return $this->hasOne(UserDetail::class);
}
I am calling the models something like that
public function index()
{
$user = User::find(2);
$user->user_details = $user->userDetails;
echo "<pre>"; print_r($user->toArray()); die;
}
But I am printing the return getting like this:-
Array
(
[id] => 2
[name] => Joney
[email] => joney#gmail.com
[email_verified_at] => 2019-04-29 02:01:03
[password] => dasDSADASDASDAS
[status] => Active
[remember_token] => DSADASd
[created_at] => 2019-04-29 00:00:00
[updated_at] => 2018-09-24 02:00:00
[user_details] => Array
(
[id] => 2
[user_id] => 2
[address] => Dhampur
[mobile_number] => 8006009195
[passport_number] => sasdadasd
[zip_code] => 201301
[created_at] => 2019-04-29 06:00:00
[updated_at] => 2019-04-29 02:00:00
)
)
So I want to get the return after merge the details into an object.
Thanks
try below code:
$user = User::with('userDetails')->find(2);
$user->user_detail_id = $user->userDetails->id;
$user->address = $user->userDetails->address;
$user->mobile_number = $user->userDetails->mobile_number;
$user->passport_number = $user->userDetails->passport_number;
$user->zip_code = $user->userDetails->zip_code;
unset($user->userDetails);
echo "<pre>"; print_r($user->toArray()); die;
Thanks.
This will happen automatically when you load the relationship. You can do this with either with() or load() depending on whether you have the model or not.
With your example above you would use with() as you're getting the user at the same time:
$user = User::with('userDetails')->find(2);
Alternatively, if you already had the User model then you would use load():
$user = User::find(2);
$user->load('userDetails');
Now, with all of that said, since you only have the one User model and not a collection, you don't need to worry about the (n+1) problem that eager loading solves so you can just access the property and Laravel will automatically getthe information for you e.g.:
$user = User::find(2);
$user->user_details;
Now if you do dd($user->toArray()) you will see that the information has been loaded.
Just an FYI, instead of using print_r() with die() and wrapping it in <pre> tags, you can use the dump() and dd() helper functions instead.
Have you try this
$user = User::find(2)
echo print-r ($user->userDetails);

Laravel Eloquent ORM relationship query

I need help, I have a problem with querying Model Relationships.
I actually know and get the job done using the query method but I'd like to know the "laravel way" of querying relationships.
Here's what's on my controller.
//HealthProfile Controller
//$id value is 2
$health_profiles = User::find($id)->with('health_profiles')->first();
problem is that the return for the query is the records for id = 1 and not id = 2. It basically ignored the "find" method. I just want to get the health profiles for a specific user_id.
[id] => 1
[firstname] => patrick
[lastname] => marino
[email] => patrick#gmail.com
[membership_code] => starpatrick
[birthdate] => 1989-05-17
[contact_number] => 1230123
[active] => 1
[created_at] => 2014-07-01 16:10:05
[updated_at] => 2014-07-01 16:10:05
[remember_token] =>
[health_profiles] => Array
(
[0] => Array
(
[id] => 1
[user_id] => 1
[parent_id] =>
[name] => patrick star
[relationship] =>
[gender] => male
[birthdate] => 1989-05-17
[marital_status] =>
[number_of_children] =>
[weigth] =>
[height] =>
[blood_type] =>
[blood_pressure] =>
[hdl] =>
[ldl] =>
[vldl] =>
[visual_activity] =>
[lifestyle] =>
[current_bmi] =>
[weight_goal] =>
[weekly_goal] =>
[rdc] =>
[created_at] => 2014-07-01 16:10:05
[updated_at] => 2014-07-01 16:10:05
)
This is my schema
//User model
public function health_profiles()
{
return $this->hasMany('HealthProfile');
}
//HealthProfile model
public function user()
{
return $this->belongsTo('User', 'user_id', 'id');
}
First a few words of explanation:
find($id) already runs the query (it uses where(id, $id)->first() under the hood) so place it at the end, because now you unwittingly did this:
User::find($id);
User::with('health_profiles')->first();
Another problem is, like you already noticed, that Eloquent won't work with this setup:
public function health_profiles() ...
$user = User::find($id);
$user->health_profiles; // null
because when loading dynamic properties (relations) it looks for camelCased method on the model.
However eager loading will work as expected:
$user = User::with('health_profiles')->find($id);
$user->health_profiles; // related model/collection
So you definitely should comply with the naming conventions if you want Eloquent to be your friend ;)
But that's not all. It will work the other way around:
public function healthProfiles() ...
$user = User::find($id);
$user->healthProfiles; // works, returns related model/collection
$user->health_profiles; // works as well, returns the model/collection
To sum up and answer your question, each of those will work for you:
// Assuming Profile is the model
// 2 queries
$user = User::find($id);
$profiles = $user->healthProfiles;
// or 1 query
$profiles = Profile::where('user_id', $id)->get();
// or 2 queries: 1 + 1 subquery
$profiles = Profile::whereHas('user', function ($q) use ($id) {
$q->where('users.id', $id);
})->get();
You can try putting the with before the find so that the builder knows to eager load that relationship on what you are trying to find.
$user = User::with('health_profiles')->find($user_id);
$health_profiles = $user->health_profiles;
Try this
User::with('health_profiles')->find($id);
I don't think you need to call the first method because find as it's stated will only find the one row of data that you need.
I've found the culprit. L4 has problems with snake cased methods! I changed it to camelCase and it worked.
$lawly = User::where('id', '=', 2)->first();
$lawly->healthProfiles->toArray()
src: https://coderwall.com/p/xjomzg

codeignite trying to get property of non-object not resolved

I am attempting to access the result set from a model query in the view. I have the following:
Controller:
$courseId = $this->session->userdata('courseId');
//echo "Course: ".$courseId;
if(isset($courseId) && $courseId != '')
{
$result = $this->Course_model->loadBasicDetailsEdit($courseId);
$data['basicCourseDetails'] = $result;
$this->load->view('course/basicDetails', $data);
}
Model:
function loadBasicDetailsEdit($courseId)
{
$this->db->select('*');
$this->db->where('course_id', $courseId);
$this->db->from('course');
$query = $this->db->get();
if ( $query->num_rows() > 0 )
{
return $query->result();
} else {
return FALSE;
}
}
and in the view I tried to print_r() and got this:
Array ( [0] => stdClass Object ( [course_id] => 8 [title] => Photography [summary] => [description] => [price] => [member_id] => 12 [category] => [audience] => [goals] => [date] => 2013-09-26 [production] => 0 ) )
I tried to access this using $basicCourseDetails->title or $basicCourseDetails['title']
but neither are working. Any hint as to why this is happening?
Regards,
try this:
foreach($basicCourseDetails as $basic){
echo($basic->title);
}
or something like this:
echo($basicCourseDetails[0]->title);
This is an array of objects
Array ( [0] => stdClass Object ( [course_id] => 8 [title] => Photography [summary] => [description] => [price] => [member_id] => 12 [category] => [audience] => [goals] => [date] => 2013-09-26 [production] => 0 ) )
Contains one stdObject in the array, so, first objects is 0, if there were more, then second item could have index 1 and so on. To retrieve data from the first (here is only one) stdobject you may use
echo $basicCourseDetails[0]->title; // title will be printed
You can send data to the view page by this line of code which is mentioned in above question.
$result = $this->Course_model->loadBasicDetailsEdit($courseId);
$data['basicCourseDetails'] = $result;
$this->load->view('course/basicDetails', $data);
But when you will access these data in view then you need to access all data one by one by using foreachloop in the view page.
For example if you have a view page like basic_details.php inside course folder then you need to write code like this to access these data.
foreach ($basicCourseDetails as $key => $value) {
$name = $value->name;
}
The above foreachloop can be written in view page where you want to access data.

Resources