vardump don't work correctly in CI - codeigniter

My controller:
function search()
{
$this->load->model('membership_model');
$this->membership_model->search();
}
Model:
function search()
{
$q = $this->db->get('feeds');
var_dump($q);
}
Why var_dump it returns me this:
object(CI_DB_mysql_result)#19 (8) { ["conn_id"]=> resource(57) of type (mysql link persistent) ["result_id"]=> resource(68) of type (mysql result) ["result_array"]=> array(0) { } ["result_object"]=> array(0) { } ["custom_result_object"]=> array(0) { } ["current_row"]=> int(0) ["num_rows"]=> int(325) ["row_data"]=> NULL }
It is normal? it not supposed to display me another format of data? Array...etc[]=>[]

to display the data you should write:
var_dump($q->result());
because:
$this->db->get();
returns an object from class CI_DB_mysql_result. which has the following fields in it:
$conn_id, $result_id, $result_array,
$result_object, $custom_result_object, $current_row, $num_rows, $row_data
which you are seeing in your var_dump

You need to convert the query result into an array of items or object of items
var_dump($q->result());
or
var_dump($q->result_array());
Using array is less memory consuming

Related

if condition in laravel query

some time some variables are null so how check the variable is not null and apply direct in query
$fiter_products = DB::table('products')->DISTINCT('modalid')->DISTINCT('brandid')->select('rimdiameter','modalid','modalname1','modalname2','image1','brand','minprice','maxprice')->where('hubbore','>=',$centre_bore)->where('boltpattern',$boltptn)->where('rimdiameter', $diameter)->where('rimwidth', $width)->where('rimwidthfront', $frontwid)->where('construction', $construct)->where('modalname2', $color)->where('brand', $brand)->get();
any way to solve this issue ?
You may pass another Closure as the third parameter to the when method. This Closure will execute if the first parameter evaluates as false.
$fiter_products = DB::table('products')
->DISTINCT('modalid')
->DISTINCT('brandid')
->select('rimdiameter','modalid','modalname1','modalname2','image1','brand','minprice','maxprice')
->when($centre_bore, function($query, $centre_bore) {
$query->where('hubbore','>=',$centre_bore);
})->when($boltptn, function($query, $boltptn) {
$query->where('boltpattern',$boltptn);
})...
You can use the whereNotNull('field) method. From the docs:
The whereNotNull method verifies that the column's value is not NULL
$product = DB::table('products')->DISTINCT('modalid')->DISTINCT('brandid')->select('rimdiameter','modalid','modalname1','modalname2','image1','brand','minprice','maxprice');
if(isset($centre_bore)){
$product->where('hubbore','>=',$centre_bore);
}
if(isset($boltptn)){
$product->where('boltpattern',$boltptn);
}
if(isset($diameter)){
$product->where('rimdiameter', $diameter);
}
if(isset($width)){
$product->where('rimwidth', $width);
}
if(isset($frontwid)){
$product->where('rimwidthfront', $frontwid);
}
if(isset($construct)){
$product->where('construction', $construct);
}
if(isset($color)){
$product->where('modalname2', $color);
}
if(isset($brand)){
$product->where('brand', $brand);
}
$fiter_products = $product->get();

Laravel 5.7 many to many sync() not working

I have a intermediary table in which I want to save sbj_type_id and difficulty_level_id so I have setup this:
$difficulty_level = DifficultyLevel::find(5);
if ($difficulty_level->sbj_types()->sync($request->hard, false)) {
dd('ok');
}
else {
dd('not ok');
}
Here is my DifficultyLevel.php:
public function sbj_types() {
return $this->belongsToMany('App\SbjType');
}
and here is my SbjType.php:
public function difficulty_levels() {
return $this->hasMany('App\DifficultyLevel');
}
In the above code I have dd('ok') it's returning ok but the database table is empty.
Try to change
return $this->hasMany('App\DifficultyLevel');
to
return $this->belongsToMany('App\DifficultyLevel');
The sync() method takes an array with the id's of the records you want to sync as argument to which you can optionally add intermediate table values. While sync($request->hard, false) doesn't seem to throw an exception in your case, I don't see how this would work.
Try for example:
$difficulty_level->sbj_types()->sync([1,2,3]);
where 1,2,3 are the id's of the sbj_types.
You can read more about syncing here.

Laravel Relationship Collection iteration returns boolean

I have a recursive relationship (sections and sub sections)
defined as this in ReportSection model:
function sub_sections() {
return $this->hasMany('App\ReportSection', 'parent_id');
}
and I'm trying to iterate through it like so:
$section = Section::find($id);
\DB::beginTransaction();
try {
foreach(ReportForm::unlockedForm($section->form_id)->get() as $report) {
foreach($report->sections()->where('section_id', $section->id)->get() as $reportSections) {
\Log::info($reportSections);
foreach($reportSections as $rSection) {
\Log::info($rSection);
foreach($rSection->sub_sections as $subSection) {
The line \Log::info($reportSections); gives {"id":3,"report_form_id":1,"name_en":"DDD","name_fr":"DDD","created_at":"2016-11-29 07:47:24","updated_at":"2016-11-29 07:47:32","section_id":118,"parent_id":1,"order":99,"hidden":0} as expected
but the iterating through it somehow gives a boolean \Log::info($rSection); gives 1
The last line foreach($rSection->sub_sections as $subSection) { gives the error 'Trying to get property of non-object'
Why would iteration through a relationship collection give a boolean? What am I doing wrong?
Edit: changed sub_sections() to sub_sections but the error is still present
You should call the attribute name not the method:
foreach($rSection->sub_sections as $subSection)
{}
Ok after taking a break I was able to figure out that the problem was I was iterating through the same collection twice.
Instead of
foreach(ReportForm::unlockedForm($section->form_id)->get() as $report) {
foreach($report->sections()->where('section_id', $section->id)->get() as $reportSections) {
foreach($reportSections as $rSection) {
It should have been
foreach(ReportForm::unlockedForm($section->form_id)->get() as $report) {
foreach($report->sections()->where('section_id', $section->id)->get() as $rSection) {

Magento product save, how to detect whether product data has been changed

While editting a product in the backend I need to know whether any of it's data has been changed or not?
$product->hasDataChanges() always return true even I didn't modify any fields.
Why does $product->hasDataChanges() always return true even I didn't modify any fields.?
Looking into the Varien_Object function setData function it appears that hasDataChanges is always set to true even if technically the data has not changes.
public function setData($key, $value=null)
{
$this->_hasDataChanges = true;
if(is_array($key)) {
$this->_data = $key;
$this->_addFullNames();
} else {
$this->_data[$key] = $value;
if (isset($this->_syncFieldsMap[$key])) {
$fullFieldName = $this->_syncFieldsMap[$key];
$this->_data[$fullFieldName] = $value;
}
}
return $this;
}
Solution:
When you have a model which is an type of Mage_Core_Model_Abstract, then you can easily get the previous data (original data) on save using public function getOrigData($key=null) method.
getOrigData() returns the data in the object at the time it was initialized/populated.
After the model is initialised you can update that data and getData() will return what you currently have in that object.
Have a look at Varien_Object (getOrigData,setOrigData) so you can have a look at how and why it is used.

Parent, Child, Child of Child in Laravel

I have three models, Province, City and Job.
Province has the following:
public function cities() {
return $this->hasmany('City');
}
City has the following:
public function province() {
return $this->belongsTo('Province', 'province_id');
}
public function jobs() {
return $this->hasmany('Job');
}
Job has the following:
public function city() {
return $this->belongsTo('City', 'city_id');
}
I am trying to get the total number of jobs in each province and the following doesn't work. Would appreciate if someone could point out what I am doing wrong?
$province->cities->jobs->count()
Thanks!
You have ready-to-use solution in Eloquent, no need for loading everything and adding up count on the collections.
// Province
public function jobs()
{
return $this->hasManyThrough('Job', 'City');
}
then just:
$province->jobs()->count();
It runs simple SELECT count(*) without loading redundant collections.
Additionally, if you need to eager load that count on the collection of provinces, then use this:
public function jobsCount()
{
return $this->jobs()->selectRaw('count(*) as aggregate')->groupBy('province_id');
}
public function getJobsCountAttribute()
{
if ( ! array_key_exists('jobsCount', $this->relations)) $this->load('jobsCount');
return $this->getRelation('jobsCount')->first()->aggregate;
}
With this you can easily get the count for multiple provinces at once (with just 2 queries executed):
$provinces = Province::with('jobsCount')->get();
foreach ($provinces as $province)
{
$province->jobsCount;
}
This is because cities is a Collection you have to loop through each of them to get the jobs number and add them up.
Within controller: Like so;
$job_count = 0;
$province->cities->each(function ($city) use ($job_count){
$job_count += $city->jobs->count();
});
The $job_count would be equal to the total number of jobs within each of it cities.
Please Note: Be sure to eager load your relations data to reduce the amount of queries that are made on your database.
$province = Province::with('cities', 'cities.jobs')...

Resources