not work scopes in Yii - 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";

Related

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

Redirect user after saving data in admin controller Prestashop

I have an admin controller which displays the list of data on clicking edit on an entry or adding a new entry I am showing renderForm() but after saving the data (either by edit or add) I want to redirect the user to another controller in place of showing the same controller list.
Bellow is the code which I am currently using for example of my requirements I trying to do something which I have done below in initcontent() but that is not working so I want to know where should I call this in place of initcontent()
<?php
/**
* The file is controller. Do not modify the file if you want to upgrade the module in future
*
* #author Globo Jsc <contact#globosoftware.net>
* #copyright 2016 Globo., Jsc
* #link http://www.globosoftware.net
* #license please read license in file license.txt
*/
include_once(_PS_MODULE_DIR_ . 'cardelivery/classes/AdditionalServicesModel.php');
class AdminAdditionalServiceController extends ModuleAdminControllerCore {
public $name;
public function __construct() {
$this->name = 'AdminAdditionalService';
$this->className = 'AdditionalServicesModel';
$this->table = 'additional_service';
$this->meta_title = $this->l('Additional Services');
$this->deleted = false;
$this->explicitSelect = true;
$this->context = Context::getContext();
$this->bootstrap = true;
$this->_defaultOrderBy = 'id_additional_service';
$this->filter = true;
if (Shop::isFeatureActive()) {
Shop::addTableAssociation($this->table, array('type' => 'shop'));
}
$this->position_identifier = 'id_additional_service';
$this->addRowAction('edit');
$this->addRowAction('delete');
$this->fields_list = array(
'id_additional_service' => array(
'title' => $this->l('ID'),
'type' => 'int',
'width' => 'auto',
'orderby' => false),
'service_name' => array(
'title' => $this->l('Icon'),
'width' => 'auto',
'orderby' => false,
),
'service_desc' => array(
'title' => $this->l('service_desc'),
'type' => 'text'
),
'active' => array(
'title' => $this->l('Status'),
'width' => 'auto',
'active' => 'status',
'type' => 'bool',
'orderby' => false),
);
parent::__construct();
}
function initContent() {
parent::initContent();
if (Tools::isSubmit('submit')) {
Tools::redirectAdmin(self::$currentIndex . '&token=' . Tools::getAdminTokenLite('AdminCategories') . '&conf=7');
}
}
public function initPageHeaderToolbar() {
$this->page_header_toolbar_btn['back_to_list'] = array(
'href' => Context::getContext()->link->getAdminLink('AdminGCardeliverycity', true),
'desc' => $this->l('Back to list', null, null, false),
'icon' => 'process-icon-back'
);
parent::initPageHeaderToolbar();
}
public function renderForm() {
$id_citydelivery = (int) Tools::getValue('id_citydelivery');
if ($id_citydelivery == 0) {
$addSerModObj = new AdditionalServicesModel((int) Tools::getValue('id_additional_service'));
$id_citydelivery = $addSerModObj->id_citydelivery;
}
$fields_form_1 = array(
'form' => array(
'legend' => array('title' => $this->l('Additional Service'), 'icon' => 'icon-cogs'),
'input' => array(
array(
'type' => 'hidden',
'name' => 'id_citydelivery'
),
array(
'type' => 'text',
'label' => $this->l('Service_name'),
'name' => 'service_name',
'size' => 255,
'required' => true,
'desc' => $this->l('Enter name of Arrival port')
),
array(
'type' => 'text',
'label' => $this->l('service_desc'),
'name' => 'service_desc',
'size' => 255,
'required' => true,
'desc' => $this->l('Enter name of Arrival port')
),
array(
'type' => 'text',
'label' => $this->l('charge'),
'name' => 'charge',
'size' => 255,
'required' => true,
'desc' => $this->l('Enter name of Arrival port')
),
array(
'type' => 'switch',
'label' => $this->l('Active'),
'name' => 'active',
'required' => false,
'is_bool' => true,
'values' => array(array(
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Active')), array(
'id' => 'active_off',
'value' => 0,
'label' => $this->l('Inactive')))),
),
'submit' => array('title' => $this->l('Save')),
'buttons' => array(
array(
'href' => Context::getContext()->link->getAdminLink('AdminGCardeliverycity', true) . '&updatecitydelivery&id_citydelivery=' . $id_citydelivery,
'title' => $this->l('Cancle'),
'icon' => 'process-icon-cancel'
)
)
)
);
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->module = $this;
$helper->name_controller = $this->name;
$helper->toolbar_scroll = true;
$lang = new Language((int) Configuration::get('PS_LANG_DEFAULT'));
$helper->default_form_language = $lang->id;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
$this->fields_form = array();
$helper->identifier = $this->identifier;
$helper->submit_action = 'submit';
$helper->currentIndex = AdminController::$currentIndex;
$helper->token = Tools::getAdminTokenLite($this->name);
$id_additional_service = (int) Tools::getValue('id_additional_service');
$additionalServiceObj = new AdditionalServicesModel($id_additional_service);
$helper->tpl_vars = array(
'fields_value' => $this->getFormValues($additionalServiceObj),
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id
);
$_1 = $helper->generateForm(array($fields_form_1));
$return = $_1;
return $return;
}
function getFormValues($additionalServiceObj) {
return array(
'service_name' => Tools::getValue('service_name ', $additionalServiceObj->service_name),
'service_desc' => Tools::getValue('service_desc', $additionalServiceObj->service_desc),
'charge' => Tools::getValue('charge', $additionalServiceObj->charge),
'active' => Tools::getValue('active', $additionalServiceObj->active)
);
}
}
First of all, it seems that you redirect your page to the same URL. Try to use this
Tools::redirectAdmin($this->context->link->getAdminLink('AdminHome'));
where
AdminHome
is a redirect URL, you need to replace it with yours.
And second, try to use
Tools::getIsset('yourButtonName')
instead of
Tools::isSubmit('yourButtonName')
and the last, if nothing mentioned above will not help, try to move
parent::initContent();
and put it after your condition
Probably redirect_after may be useful
$this->redirect_after = 'Your custom address';

Show a pdf on CakePhp after creating and seving it

it's my first post so sorry in case i will do some mistakes. I am getting mad from this morning because of this function:
public function viewpdf0($agent_id = null, $month = null, $year = null){
if(!empty($agent_id)){
$this->create_remittance($agent_id, $month, $year);
$this->layout = '/pdf/default';
$this->render('/Pdf/remittance_0');
sleep(1);
$this->redirect('/files/remittances/STAT_'.str_pad($month, 2, '0', STR_PAD_LEFT).'_'.str_pad($year, 0, '0', STR_PAD_LEFT).'_'.$agent['Registry']['hash'].'_TEMP.pdf');
}
}
The problem started when i decided to take off the calculations from the viewpdf0 function and putting it into the create_remittance function.
When i click on the button "Print Pdf" i get the following error:
Missind Controller for files. This mean that the Cakephp is processing the redirect thinking to find a controller called files!!!!
It's very strange, because if i put again the calculation functions directly inside the viewpdf0 function it works.
Below part of the create_remittance function:
public function create_remittance($agent_id = null, $month = null, $year = null) {
$agent = $this->Registry->findById($agent_id);
$this->set('agent', $agent);
$conditions = array();
array_push($conditions , array('Dossier.active' => '1'));
array_push($conditions , array('Edossier.active' => '1'));
array_push($conditions , array('OR' =>
array(
array(
'Dossier.item_parent_id' => '43'
),
array(
'Dossier.item_parent_id' => '44'
)
)
));
//20:POLIZZA EMESSA - 17:PERFEZIONATA - 14:ATTESA DOC. ORIGINALI - 18:CARICATA
array_push($conditions , array('Dossier.state_id' => array(20,17,14,18)));
array_push($conditions , array('EXTRACT(MONTH FROM "Edossier"."date_emissione") =' => $month));
array_push($conditions , array('EXTRACT(YEAR FROM "Edossier"."date_emissione") =' => $year));
array_push($conditions , array('Dossier.agent_id' => $agent_id));
$query = array(
'fields' => array( 'DISTINCT Dossier.id',
'Agent.surname'
),
'joins' => array(
array('table' => 'items',
'alias' => 'Item',
'type' => 'LEFT',
'conditions' => array('Dossier.item_id = Item.id'),
),
array('table' => 'edossiers',
'alias' => 'Edossier',
'type' => 'LEFT',
'conditions' => array('Edossier.dossier_id = Dossier.id'),
),
array('table' => 'registries',
'alias' => 'Registry',
'type' => 'LEFT',
'conditions' => array('Dossier.registry_id = Registry.id'),
),
array('table' => 'registries',
'alias' => 'Agency',
'type' => 'LEFT',
'conditions' => array('Dossier.insurance_agency_id = Agency.id'),
),
array('table' => 'registries',
'alias' => 'Company',
'type' => 'LEFT',
'conditions' => array('Dossier.insurance_company_id = Company.id'),
),
array('table' => 'states',
'alias' => 'State',
'type' => 'LEFT',
'conditions' => array('Dossier.state_id = State.id'),
),
array('table' => 'categories',
'alias' => 'Product',
'type' => 'LEFT',
'conditions' => array('Dossier.item_parent_id = Product.id'),
),
array('table' => 'registries',
'alias' => 'Agent',
'type' => 'LEFT',
'conditions' => array('Dossier.agent_id = Agent.id'),
),
array('table' => 'states',
'alias' => 'RenewedState',
'type' => 'LEFT',
'conditions' => array('Dossier.renewed_state_id = RenewedState.id'),
),
),
'conditions' => $conditions,
'recursive' => -1
);
$dossiers = $this->Dossier->find('all', $query);
$this->set('dossiers', $dossiers);
$debug_dossier = '';
$edossiers = array();
$edossiers3 = array();
$dossier_ids = array();
$total_gross = 0;
$total_taxes = 0;
$total_equity = 0;
$total_fees_gross = 0;
$total_bill_tax = 0;
foreach($dossiers as $dossier){
// Query su type=1 ossia rata di pagamento
$edossier_all = $this->Edossier->find('all', array(
'conditions' => array(
'Edossier.active' => '1',
'Edossier.dossier_id' => $dossier['Dossier']['id'],
'Edossier.type' => '1',
'Edossier.month' => $month,
'Edossier.year' => $year
),
'order' => array(
'Edossier.id ASC'
)
));
// Query su type=2 ossia storno di pagamento
$edossier3_all = $this->Edossier->find('all', array(
'conditions' => array(
'Edossier.active' => '1',
'Edossier.dossier_id' => $dossier['Dossier']['id'],
'Edossier.type' => '2',
'Edossier.month' => $month,
'Edossier.year' => $year
),
'order' => array(
'Edossier.id ASC'
)
));
foreach($edossier_all as $edossier){
if(!empty($edossier)){
$item = $this->Item->findById($edossier['Dossier']['item_id']);
$agent_name = $this->Registry->findById($edossier['Dossier']['agent_id']);
$state = $this->State->findById($edossier['Dossier']['state_id']);
$registry = $this->Registry->findById($edossier['Dossier']['registry_id']);
$cashflow = $this->Cashflow->findByHash($edossier['Edossier']['cashflow_hash']);
$edossier['Agent'] = $agent_name;
$edossier['Item'] = $item;
$edossier['State'] = $state;
$edossier['Registry'] = $registry;
$edossier['Cashflow'] = $cashflow;
array_push($edossiers, $edossier);
$total_gross = $total_gross + $edossier['Edossier']['gross'];
$total_taxes = $total_taxes + $edossier['Edossier']['taxes'];
$total_equity = $total_equity + $edossier['Edossier']['equity'];
$total_fees_gross = $total_fees_gross + $edossier['Edossier']['fees_gross'];
$total_bill_tax = $total_bill_tax + $edossier['Edossier']['bill_tax'];
}
}
foreach($edossier3_all as $edossier3){
if(!empty($edossier3)){
$total_gross = $total_gross - $edossier3['Edossier']['value'];
$total_taxes = $total_taxes - $edossier3['Edossier']['taxes'];
$total_equity = $total_equity - $edossier3['Edossier']['equity'];
$total_fees_gross = $total_fees_gross - $edossier3['Edossier']['fees_gross'];
$total_bill_tax = $total_bill_tax - $edossier3['Edossier']['bill_tax'];
array_push($edossiers3, $edossier3);
}
}
array_push($dossier_ids, $dossier['Dossier']['id']);
$debug_dossier .= $dossier['Dossier']['id'] .',';
}
//Trovo tutte le rate pagate per conteggiare il totale pagato, nelle successive righe
$edossiers2 = $this->Edossier->find('all', array(
'conditions' => array(
'Edossier.active' => '1',
'Edossier.dossier_id' => $dossier_ids,
'Edossier.payed' => '1',
'Edossier.month' => $month,
'Edossier.year' => $year
)
));
$edossier_ids = array();
foreach($edossiers as $edossier){
array_push($edossier_ids, $edossier['Edossier']['id']);
}
$total_payed = 0;
foreach($edossiers2 as $edossier2){
//$cashflow = $this->Cashflow->findByHash($edossier2['Edossier']['cashflow_hash']);
//$total_payed = $total_payed + $cashflow['Cashflow']['qty'];
$cashflows = $this->Cashflow->find('all', array(
'conditions' => array(
'Cashflow.edossier_id' => $edossier2['Edossier']['id'],
'Cashflow.active' => '1'
)
));
foreach ($cashflows as $cashflow) {
$total_payed = $total_payed + $cashflow['Cashflow']['qty'];
}
}
$this->set('total_gross', $total_gross);
$this->set('total_taxes', $total_taxes);
$this->set('total_equity', $total_equity);
$this->set('total_fees_gross', $total_fees_gross);
$this->set('total_bill_tax', $total_bill_tax);
$this->set('total_payed', $total_payed);
$this->set('edossiers3', $edossiers3);
$this->set('edossiers2', $edossiers2);
$this->set('edossiers', $edossiers);
$this->set('agent', $agent);
$this->set('dossier_ids', $dossier_ids);
$this->set('edossier_ids', $edossier_ids);
$this->set('month', $month);
$this->set('year', $year);
$this->set('debug_dossier', $debug_dossier);
}
I solved the issue just inserting an array return in the function create_remittance and in the function viewpdf0 i fixed in this way:
$remittance = $this->create_remittance($agent_id, $month, $year);
Thank you.

Yii cactivedataprovider count duplicates record and all record count

$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',
),
),
)
));

Yii ClistView pagination not working

My Clistview Pagination is not working properly.I tried every thing but still not getting success.Here is my model function:
public function getallone()
{
$criteria = new CDbCriteria;
$criteria->select = "t.id";
$criteria->condition = "t.featured=1 AND t.status = 1";
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'pagination' => array('pageSize' => 25),
));
}
Am still getting only 10 items per page but i want 25.I tried to 2 items but its also not working. Whats wrong with that?Didnt find.
And this is my view code:
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$model->getallone(),
'itemView'=>'_allone',
'enablePagination' => true,
)); ?>
You shoud try this for better pagination option
$condition='type=:type';
$dataArray[':type']=$type;
$dataProvider=new CActiveDataProvider('modelname', array(
'criteria'=>array(
'select'=>'*',
'condition'=>$condition,
'params'=>$dataArray,
'order'=>'id DESC',
'offset'=>($page-1)*$limit,
'limit'=>$limit,
),
'pagination'=>array(
'pageSize'=>$limit,
),
));
$totalActivity= $dataProvider->getTotalItemCount();
http://www.yiiframework.com/doc/api/1.1/CActiveDataProvider for reference
try and use CArrayDataProvider
here is how do it:
public function actionIndex() {
$command = Yii::app()->db->createCommand("
SELECT u.id as u_id, u.link as u_link, u.title as u_title, u.description as u_description, u.public as u_public, u.created as u_created, u.status as u_status, w.id as w_id, w.domain as w_domain, w.status as w_status
from url as u
left join website as w
on w.id = u.website_id
where u.title!='null' and u.status = 1 and w.status = 1 and u.public = 1;
");
$news = $command->queryAll();
$dataProvider = new CArrayDataProvider($news, array(
'id' => 'user',
'keyField' => 'u_id',
'sort' => array(
'defaultOrder' => 'u_id desc',
),
'pagination' => array(
'pageSize' => 10,
),
));
$model_website = Website::model()->findAllByAttributes(array('status' => 1), array('limit' => 30, 'order' => 'id desc'));
$this->render('index', array(
'model_website' => $model_website,
'dataProvider' => $dataProvider,
));
}
and the clistview:
$this->widget('zii.widgets.CListView', array(
'dataProvider' => $dataProvider,
'template' => "{summary}\n{pager}\n{items}\n{summary}\n{pager}",
'itemView' => '_index',
'pager' => array(
'maxButtonCount' => 10,
),
)
);