Can anyone tell the difference between find and findOne in yii2?


(MuhammedSami) #22

non, there isn’t something related to this error in php logs neither.


(MuhammedSami) #23
$data_model = $model_service_request->getEntityModel();

    if (!isset($data_model)) {
        $entity_classname = $model_service_request->getEntityClassName();
        $model = $entity_classname;
        $data_model = new $model();
    }

heer Im trying to get the related model and instantiate it.
I have ‘app\models\IndividualTrader’ as classname, but when doing it I get again this error
" Invalid argument supplied for foreach() " don’t know why it happens but there is not a foreach run from the code, I have changed my rules red multiple time to make sur it’s okay, but there is something surely not working with my Model.
What happens when I do new $model()
there is init() in the model but I removed and nothing changed. I get this error wherever I try to use the model app\models\IndividualTrader().

thanks in advance for your help.

My child Model:
<?php

namespace app\models;

use app\components\Defaults;
use Yii;
use yii\helpers\ArrayHelper;

class IndividualTrader extends Entreprise
{
const PROCESSING_IFU = 'processing_ifu';

public static function model($className = __CLASS__)
{
    return parent::model($className);
}

public function rules()
{
    return ArrayHelper::merge(parent::rules(),
        [
            [['emplois_generes','ca'], 'required', 'on' => self::SCENARIO_FORM],
            [['identifiant_fiscal'], 'required', 'on' => self::PROCESSING_IFU],
            [['activite'], 'required', 'on' => self::SCENARIO_ACTIVITY],
            [['forme_juridique'], 'default', 'value' => FormeJuridique::PP],
            [['emplois_generes'], 'number', 'min' => 0],

            [['objet_social'], 'default', 'value' => ''],
            //  [['identifiant_fiscal, activite, ville_taxe_professionnelle'], 'required', 'on' => 'fisc'],
            [['raison_sociale', 'identifiant_fiscal', 'registre_commerce'], 'unique'],
            /*[['has_nationalite'],'safe','on'=>self::SCENARIO_GUIDE],*/
            [['registre_commerce_declare', 'date_rc'], 'validateRequiredRegistrationData', 'registration_attribute' => 'has_already_registered_rccm', 'validate_if_value' => 1, 'on' => self::SCENARIO_FORM],
            [['telephone'], 'ext.LPNValidator.LPNValidator', 'defaultCountry' => Defaults::COUNTRY_PHONE_CODE, 'on' => self::SCENARIO_CONTACT],
            [['prenom_contact', 'nom_contact', 'telephone', 'email'], 'required', 'on' => self::SCENARIO_CONTACT],
            [['activite'], 'required', 'on' => self::SCENARIO_FORM],
            [['activite_secondaire'], 'compare', 'compareAttribute' => 'activite', 'operator' => '!=', 'message' => Yii::t('app', "{compareAttribute} and {attribute} must not be equal"), 'on' => self::SCENARIO_FORM],
            [['trade_name,company_name'], 'safe', 'on' => TradeNameSearch::SCENARIO_TRADE_NAME_SEARCH]
        ]
    );
}

public function init()
{
    parent::init();
    // initialize attributes with default values
    $this->debut_activite = date('d-m-Y');
    $this->forme_juridique = FormeJuridique::PP;
    $this->date_fin_activite = '31-12';
    $this->duree_activite = 99;
}

/**
 * @param $name
 * @param string $operator
 * @return CDbCriteria
 */

public function getFullNameCriteria($name, $service_request_alias, $operator = 'AND')
{
    $criteria = new CDbCriteria;
    //    $criteria->select = "(select CONCAT(pp.nom , CONCAT('|', CONCAT(pp.prenom 
 ,CONCAT('|',ent.nom_commercial)))) from tbl_personne_physique pp, tbl_entreprise ent where pp.id = 
    ent.pp_commercant and ent.service_request = ".$t.".id) as full_name";
    $full_name = "(select CONCAT(pp.nom , '|' , pp.prenom, '|' ,ent.nom_commercial) from 
   tbl_personne_physique pp, tbl_entreprise ent where pp.id = ent.pp_commercant and 
  ent.service_request = " . $service_request_alias . ".id)";
    $criteria->compare($full_name, $name, true, $operator);
    return $criteria;

   }
 }

(Imanie383) #24

find()->where()->one() is the same findOne($PrimaryKey)

In both you don’t have an array, the result is load in the model
$model = findOne(1);
echo $model->name;
echo $name->age;

if you want an array, you can do this:

$customers = Customer::find()->asArray()->all();
foreach($customers as $customer)

or

foreach (Customer::find()->batch(10) as $customers) {
// $customers is an array of 10 or fewer Customer objects
}

// fetch 10 customers at a time and iterate them one by one
foreach (Customer::find()->each(10) as $customer) {
// $customer is a Customer object
}

// batch query with eager loading
foreach (Customer::find()->with(‘orders’)->each() as $customer) {
// $customer is a Customer object with the ‘orders’ relation populated
}

https://www.yiiframework.com/doc/guide/2.0/en/db-active-record