Yii cactivedataprovider count duplicates record and all record count - yii

$criteria = new CDbCriteria;
$criteria->group = 't.blahah_id';
$result = new CActiveDataProvider('SocialnetworkSpams', array(
'criteria' => $criteria,
));
echo "<pre>";
print_r($result->getData());
Array
(
[0] => SocialnetworkSpams Object
(
[total] =>
[_new:CActiveRecord:private] =>
[_attributes:CActiveRecord:private] => Array
(
[id] => 1
[created_date] => 2015-12-04 03:50:47
[feedtype] => 1
[feedback] =>
[spam_status] => 1
[approveby_id] => 1
[blahah_id] => 1
[bunit_id] => 3
[spamby_id] => 1
)
[_related:CActiveRecord:private] => Array
(
)
[_c:CActiveRecord:private] =>
[_pk:CActiveRecord:private] => 1
[_alias:CActiveRecord:private] => t
[_errors:CModel:private] => Array
(
)
[_validators:CModel:private] =>
[_scenario:CModel:private] => update
[_e:CComponent:private] =>
[_m:CComponent:private] =>
)
[1] => SocialnetworkSpams Object
(
[total] =>
[_new:CActiveRecord:private] =>
[_attributes:CActiveRecord:private] => Array
(
[id] => 4
[created_date] => 2015-12-04 04:24:07
[feedtype] => 5
[feedback] => a
[spam_status] => 0
[approveby_id] =>
[blahah_id] => 2
[bunit_id] => 3
[spamby_id] => 1
)
[_related:CActiveRecord:private] => Array
(
)
[_c:CActiveRecord:private] =>
[_pk:CActiveRecord:private] => 4
[_alias:CActiveRecord:private] => t
[_errors:CModel:private] => Array
(
)
[_validators:CModel:private] =>
[_scenario:CModel:private] => update
[_e:CComponent:private] =>
[_m:CComponent:private] =>
)
)
I want duplicate record get using blahah id you can check bellow image:

Thanks
I have been resolved this issue.
$criteria = new CDbCriteria;
$criteria->select = '*, count(*) as total';
$criteria->group = 't.blahah_id';
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
View Page :
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'spam-user-grid',
'itemsCssClass' => 'table table-striped table-bordered table-hover',
'dataProvider' => $model->search(),
'template' => "{items}{pager}",
'pager' => array(
'header' => '',
),
'columns' => array(
array(
'header' => 'Blahah',
'type' => 'raw',
'value' => 'ucfirst($data->ratingprofile->name)',
//'filter' => CHtml::textField('User[name]'),
),
array(
'header' => 'Total',
'type' => 'raw',
'value' => '$data->total',
),
),
)
));

Related

How to sub query a select into another select and put inside a variable with laravel?

I want to make a select that solves the fact that I have to perform two validations one for each table, what i'm doing now is validating the $table1 and validating the $table2 ,i just want to validate $table2
$table1 = DB::table('name_table1')
->where('invoice_id','{$table2}.id')
->get();
$table2 = DB::table('name_table2')
->select('name','invoices','{$table} as itens_of_invoice')
->get();
dd($table);
And return me something like;
Array (
[0] => Object(
'name' => 'value_name',
'invoices' => 'value_invoices',
'item_of_invoice' =>
Array(
[0] => Array(
'name_item' => 'value_name',
'price' => 'value_price'
)
)
)
[1] => Object(
'name' => 'value_name',
'invoices' => 'value_invoices',
'item_of_invoice' =>
Array(
[0] => Array(
'name_item' => 'value_name',
'price' => 'value_price'
)
)
)
[2] => Object(
'name' => 'value_name',
'invoices' => 'value_invoices',
'item_of_invoice' =>
Array(
[0] => Array(
'name_item' => 'value_name',
'price' => 'value_price'
)
)
)
)
I don't find any solution if someone ever did this please help me

ManyToMany left join query builder

I have entity A with ManyToMany doctrine relation wite entity B
I want to get data of the entity A using query builder in this way
$data = array(
0 => array(
'entity_A_field_1' => 'entity_A_field_1_value',
'entity_A_field_2' => 'entity_A_field_2_value',
'entity_A_field_3' => 'entity_A_field_3_value',
'entity_B' => array(
0 => array(
'entity_B_field_1' => 'entity_B_field_1_value',
'entity_B_field_2' => 'entity_B_field_2_value',
'entity_B_field_3' => 'entity_B_field_3_value',
),
2 => array(
'entity_B_field_1' => 'entity_B_field_1_value',
'entity_B_field_2' => 'entity_B_field_2_value',
'entity_B_field_3' => 'entity_B_field_3_value',
),
),
),
1 => array(
'entity_A_field_1' => 'entity_A_field_1_value',
'entity_A_field_2' => 'entity_A_field_2_value',
'entity_A_field_3' => 'entity_A_field_3_value',
'entity_B' => null
),
);
her is my entities: entity A is product
class Product
{
/**
* #ORM\ManyToMany(targetEntity="Category", inversedBy="products",cascade={"persist", "remove"})
*
* #ORM\JoinTable(
* name="store_product_category"
* )
*/
private $categories;
Entity B is category
class Category
{
/**
* #ORM\ManyToMany(targetEntity="Product", mappedBy="categories")
*/
private $products;
I want to get all my products with their categories using query builder and without conditions (if they have| null if they don't). i need to get them all.
$data = array(
0 => array(
'product_id' => 'product_id_value',
'product_name' => 'product_name_value',
'product_reference' => 'product_reference_value',
'categories' => array(
0 => array(
'category_id' => 'category_id_value',
'category_name' => 'category_name_value',
'category_token' => 'category_token_value',
),
2 => array(
'category_id' => 'category_id_value',
'category_name' => 'category_name_value',
'category_token' => 'category_token_value',
),
),
),
1 => array(
'product_id' => 'product_id_value',
'product_name' => 'product_name_value',
'product_reference' => 'product_reference_value',
'category' => null
),
);

ActiveRecord result return by findAll method needs in different format

I executed following statement
$posts = Post::model()->with(array( 'category', 'member', 'profile' ))->findAll();
I printed $posts in log file. I got following result.
Array
(
[0] => Post Object
(
[_new:CActiveRecord:private] =>
[_attributes:CActiveRecord:private] => Array
(
[id] => 1
[post_text] => Sales needs to be increase by the end of this month. I don't wnat any reason behind this.
[member_id] => 2
[category_id] => 3
[published] => 0
[draft] => 1
[date_added] => 2014-04-06
)
[_related:CActiveRecord:private] => Array
(
[category] => Category Object
(
[_new:CActiveRecord:private] =>
[_attributes:CActiveRecord:private] => Array
(
[id] => 1
[category_name] => Anything
[type] => General
[date_added] => 2014-04-05
)
[_related:CActiveRecord:private] => Array
(
)
[_c:CActiveRecord:private] =>
[_pk:CActiveRecord:private] => 1
[_alias:CActiveRecord:private] => t
[_errors:CModel:private] => Array
(
)
[_validators:CModel:private] =>
[_scenario:CModel:private] => update
[_e:CComponent:private] =>
[_m:CComponent:private] =>
)
[member] => Member Object
(
[_new:CActiveRecord:private] =>
[_attributes:CActiveRecord:private] => Array
(
[id] => 1
[screen_name] => prashantb
[email] => prashantbharambe22#gmail.com
[date_added] => 2014-04-05
[first_name] => prashant
[last_name] => bharambe
)
[_related:CActiveRecord:private] => Array
(
[profile] => MemberProfile Object
(
[_new:CActiveRecord:private] =>
[_attributes:CActiveRecord:private] => Array
(
[id] => 1
[member_id] => 1
[city] => kalyan
[state] => maharashtra
[country] => india
[designation] => php developer
[date_added] => 2014-04-05
)
[_related:CActiveRecord:private] => Array
(
)
[_c:CActiveRecord:private] =>
[_pk:CActiveRecord:private] => 1
[_alias:CActiveRecord:private] => t
[_errors:CModel:private] => Array
(
)
[_validators:CModel:private] =>
[_scenario:CModel:private] => update
[_e:CComponent:private] =>
[_m:CComponent:private] =>
)
)
[_c:CActiveRecord:private] =>
[_pk:CActiveRecord:private] => 1
[_alias:CActiveRecord:private] => t
[_errors:CModel:private] => Array
(
)
[_validators:CModel:private] =>
[_scenario:CModel:private] => update
[_e:CComponent:private] =>
[_m:CComponent:private] =>
)
)
[_c:CActiveRecord:private] =>
[_pk:CActiveRecord:private] => 1
[_alias:CActiveRecord:private] => t
[_errors:CModel:private] => Array
(
)
[_validators:CModel:private] =>
[_scenario:CModel:private] => update
[_e:CComponent:private] =>
[_m:CComponent:private] =>
)
)
But I need a result in following format.
Array
(
[0] => Array
(
[id] => 1,
[post_text] => Sales needs to be increase by the end of this month. I don't wnat any reason behind this,
[member_id] => 2,
[category_id] => 3,
[published] => 0,
[draft] => 1,
[date_added] => 2014-04-06,
[category] => Array (
[id] => 1,
[category_name] => Anything,
[type] => General,
[date_added] => 2014-04-05,
),
[member] => Array (
[id] => 1,
[screen_name] => prashantb,
[email] => prashantbharambe94#gmail.com,
[date_added] => 2014-04-05,
[first_name] => prashant,
[last_name] => bharambe,
[profile] => Array(
[id] => 1
[member_id] => 1
[city] => kalyan
[state] => maharashtra
[country] => india
[designation] => php developer
[date_added] => 2014-04-05
)
)
)
)
In short I need all attributes values in one array. ( Model Attributes and its related model attributes )
I am new to yii. Please help me on this.
Yii doesn't work with arrays. Each DB record is represented by an ActiveRecord object. This way you can access your column values like this:
echo $model->post_text; // Sales needs to be increase...
echo $model->date_added; // 2014-04-06
echo $model->etc...
If you did set up relations right, you can access that data like this:
echo $model->category->category_name; // Anything
echo $model->member->screen_name; // prashantb
You have to create the array you showed manually. If you still need the data in an array, you can also access the attributes value:
print_r($model->attributes); // array( [id] => 1, [post_text] => 'Sales needs to be increase', ...)

not work scopes in Yii

Why my scopes not work?
$criteria = new \CDbCriteria();
$criteria->addCondition('user_id = :user_id');
$criteria->scopes = array(
'applicant' => array(
'scopes' => array('deletedStatus'),
'params' => array(':deletedStatus' => 0)
),
'filePair' => array(
'with' => array(
'category' => array(
'with' => array(
'parent' => array(
'order' => 'parent.order'
)
),
'scopes' => array('enabled')
),
'file' => array(
'scopes' => array('enabled'),
'order' => 'file.order'
)
)
)
);
$criteria->params = array(':user_id' => \Yii::app()->userApp->id);
/** #var \ApplicantDocument[] $models */
$models = \ApplicantDocument::model()->findAll($criteria);
public function scopes()
{
$t = $this->getTableAlias(false, false);
return array(
'deletedStatus' => array(
'condition' => $t.'.is_deleted = :deletedStatus'
),
'applicant' => array(
'condition' => $t.'.role = :role',
'params' => array(':role' => self::APPLICATION_STATUS_APPLICANT)
),
'guarant' => array(
'condition' => $t.'.role = :role',
'params' => array(':role' => self::APPLICATION_STATUS_GUARANTOR)
),
'company' => array(
'condition' => $t.'.role = :role',
'params' => array(':role' => self::APPLICATION_STATUS_COMPANY)
),
'trust' => array(
'condition' => $t.'.role = :role',
'params' => array(':role' => self::APPLICATION_STATUS_TRUST)
),
);
}
This scope does not work, aplicants with is_deleted selected from db
'applicant' => array(
'scopes' => array('deletedStatus'),
'params' => array(':deletedStatus' => 0)
),
Thanks for help!
Scopes not working, coz it call in ApplicantDocument, not in applicant. So i am try it solution and it help me.
$criteria->with = array('applicant');
$criteria->together = true;
$criteria->condition = "applicant.is_deleted = 0";

Populating CGridView one to many relation

In my project one of the model named types having multiple relation to other models the criteria with part is giving below its like
$criteria->with = array(
'category',
'subCategory',
'accountsSupplierPriceDetails' => array(
'with' => 'serviceLevel'
)
);
the relationship goes like
types - category - relation(1-1)
types - subcategory - relation(1-1)
types - accountsSupplierPriceDetails - relation(1-1)
accountsSupplierPriceDetails - serviceLevel - relation(1-n)
my problem is how to display the names from each table in a grid. the result of (1-n) should be displayed in a dropdownbox. when i try to access the data from column it shows the error Trying to get property of non-object ($data->accountsSupplierPriceDetails->serviceLevel ->name). can anyone please help me.
Thanks in advance.
in my view i create a cgridview with 5 rows in which one row data is populated using dropdownlist.
<div style="width:700px; height:auto;">
<?php
$widget = $this->widget('zii.widgets.grid.CGridView', array(
'id' => 'response-grid',
'dataProvider' => $pickdataset->pickCategorylistingForMain(),
'cssFile' => Yii::app()->baseUrl . '/media/css/gridview.css',
'summaryText' => '',
'ajaxUpdate' => 'response-grid',
'enablePagination' => true,
'pager' => array(
'class' => 'LinkPager',
'cssFile' => false,
'header' => false,
'firstPageLabel' => 'First',
'prevPageLabel' => 'Previous',
'nextPageLabel' => 'Next',
'lastPageLabel' => 'Last',
),
'columns' => array(
array(
'name' => 'id',
'header' => '#',
'value' => '$this->grid->dataProvider->pagination->currentPage * $this->grid->dataProvider->pagination->pageSize + ($row+1)',
),
array(
'type' => 'raw',
'name' => 'categoryExt',
'header' => 'Category',
),
array(
'type' => 'raw',
'header' => 'Sub Category',
'value' => '$data->subCategory->name',
),
array(
'type' => 'raw',
'name' => 'locationExt',
'header' => 'Location',
),
array(
'type' => 'raw',
'header' => 'Name',
'value' => 'CHtml::dropDownList($data->corporate_id."-".$data->category_id."-".$data->sub_category_id."-".$data->location_id,"",popDropBox($data->masterCategoryServiceLevels), array("class" => "listbox"), array("empty" => "Select a Location"))',
),
array(
'type' => 'raw',
'header' => 'Pick',
'value' => 'CHtml::image(Yii::app()->baseUrl . "/media/images/edit_icon.png",$data->corporate_id."-".$data->category_id."-".$data->sub_category_id."-".$data->location_id,array("title" => "Select this Combination"))',
),
),));
function popDropBox($data)
{
$list = array();
foreach ($data as $row)
{
$list[$row->serviceLevel->id] = $row->serviceLevel->name;
}
return $list;
}
?>
</div>
the relation in model is:
'masterCategoryServiceLevels' => array(self::HAS_MANY, 'MasterCategoryServiceLevel', 'category_template_id'),
'corporate' => array(self::BELONGS_TO, 'AccountsCorporate', 'corporate_id'),
'category' => array(self::BELONGS_TO, 'MasterCategory', 'category_id'),
'subCategory' => array(self::BELONGS_TO, 'MasterCategory', 'sub_category_id'),
'location' => array(self::BELONGS_TO, 'MasterLocation', 'location_id'),
the row is
array(
'type' => 'raw',
'header' => 'Name',
'value' => 'CHtml::dropDownList($data->corporate_id."-".$data->category_id."-".$data->sub_category_id."-".$data->location_id,"",popDropBox($data->masterCategoryServiceLevels), array("class" => "listbox"), array("empty" => "Select a Location"))',
),
and i use a function to create dropdown data is
function popDropBox($data)
{
$list = array();
foreach ($data as $row)
{
$list[$row->serviceLevel->id] = $row->serviceLevel->name;
}
return $list;
}
please comment if you have any doubt. i am free to share... have a nice day SO friends... wishes and prayers.