This is my controller:
public function index($mid,$payload){
$search = $payload['search'];
$users = DB::select('SELECT a.id, a.alternate_id, a.setujuterma, a.mykad, a.nama, a.email, a.notel, a.etunai,
b.ranktitle, c.ranktitle AS appointed_rank, d.nama as hirarki, e.alternate_id as placement,
e.nama as leadername, a.akses, a.suspendreason, a.regstamp,
a.matagajet, f.display as hirarkidisplay, IF(a.mykadverify = "3","1","0") as mykadverifydecode
FROM pengguna as a
LEFT JOIN penggunarank b ON a.effective_rank = b.id
LEFT JOIN penggunarank c ON a.appointed_rank = c.id
LEFT JOIN hirarki d ON a.userrank = d.id
LEFT JOIN pengguna e ON a.placement = e.id
LEFT JOIN hirarkimid f ON a.userrank = f.hirarki AND a.mid = f.mid
WHERE a.mid ='. $mid .' AND a.akses != -1'
);
$sortUser = collect($users)->sortByDesc('alternate_id')->toArray();
$collection = collect($sortUser);
$count = count($users);
// SEARCH BOX
if ($search) {
$collection->where(function ($q) use ($search) {
$q->where("alternate_id","LIKE","%{$search}%")
->orWhere("nama","LIKE","%{$search}%")
->orWhere("mykad","LIKE","%{$search}%")
->orWhere("notel","LIKE","%{$search}%")
->orWhere("email","LIKE","%{$search}%");
});
}
return [
$user,
$count
];
}
So,
$users return an array.
$collection return collection
for the search box, if I use $users, I get error
"Call to a member function where() on Array"
and if I use $collection, I get
message: "explode() expects parameter 2 to be string, object given", exception: "ErrorException",…}
Any help would be greatly appreciated. Thanks.
public function search(Request $payload){
$search = $payload['search'];
if($search == "")
{
$users = Payee::whereNotNull('payee_name')->take(10)->get();
}
else
{
$users = Payee::whereNotNull('payee_name')
->where(function ($q) use ($search) {
$q->where("payee_name","LIKE","%$search%")
->orWhere("payee_nick_name","LIKE","%$search%");
})->take(10)->get();
}
return [
$users,
];
}
I found an answer to my question. All I need is to change the query into eloquent model class. There is no other way if I want to use the where() function for my search. First I create model User.php:
<?php
namespace App;
use App\WithdrawEcash;
use Laravel\Sanctum\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Propaganistas\LaravelPhone\PhoneNumber;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use HasApiTokens, Notifiable, HasFactory;
protected $table = 'pengguna';
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $guarded = ['id'];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* #var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
// user registered by
public function userRegby()
{
return $this->belongsTo(User::class, 'regby');
}
// user leader
public function userPlacement()
{
return $this->belongsTo(User::class, 'placement');
}
public function penggunaRank()
{
return $this->belongsTo(PenggunaRank::class, 'effective_rank');
}
public function appointedRankUser()
{
return $this->belongsTo(PenggunaRank::class, 'appointed_rank');
}
public function penyatabonus()
{
return $this->belongsTo(User::class, 'id', 'pengguna');
}
//Hirarkimid userrank
public function userHirarki()
{
return $this->belongsTo(Hirarkimid::class, 'userrank', 'hirarki');
}
public function userhirarchy()
{
return $this->belongsTo(Hierarchy::class, 'userrank')->select('id', 'nama');
}
public function systemHirarki()
{
return $this->belongsTo(Hierarchy::class, 'userrank');
}
// user order
public function userOrders()
{
return $this->hasMany(Order::class, 'pengguna');
}
// user order for registration report
public function userOrder()
{
return $this->hasOne(Order::class, 'pengguna');
}
public function hierarchy()
{
$hirarki = $this->belongsTo(Hirarkimid::class, 'userrank', 'hirarki')
->select('hirarki', 'display', 'shownilaibelian', 'show_harga_ketika_pesanan')
->where('mid', auth()->user()->mid);
if ($hirarki) {
return $hirarki;
} else {
return $this->belongsTo(Hierarchy::class, 'userrank')->select('id', 'nama');
}
}
public function myCartLists()
{
return $this->hasMany(AddToCart::class, 'user_id');
}
public function bonusStatement()
{
return $this->hasMany(PenyataBonus::class, 'pengguna');
}
public function currentBonusStatement()
{
return $this->hasMany(PenyataBulanSemasa::class, 'pengguna');
}
public function withdrawEcash()
{
return $this->hasMany(WithdrawEcash::class, 'pengguna');
}
public function fileupload()
{
return $this->morphOne(FileUpload::class, 'file_upload');
}
public function fileuploads()
{
return $this->morphMany(FileUpload::class, 'file_upload');
}
public function voucherdetail()
{
return $this->hasMany(Voucherdetail::class, 'pengguna');
}
public function countryCode()
{
return $this->hasOne(Negara::class, 'nama', 'negara')->value('kod');
}
public function setNotelAttribute($value)
{
if (!is_null($value)) {
$country_code = $this->countryCode() != '' ? $this->countryCode() : 'MY';
$this->attributes['notel'] = PhoneNumber::make($value, $country_code)
->formatForMobileDialingInCountry($country_code);
} else
$this->attributes['notel'] = $value;
}
public function setNotelcsAttribute($value)
{
if (!is_null($value)) {
$country_code = $this->countryCode() != '' ? $this->countryCode() : 'MY';
$this->attributes['notelcs'] = PhoneNumber::make($value, $country_code)
->formatForMobileDialingInCountry($country_code);
} else
$this->attributes['notelcs'] = $value;
}
}
And in my controller I simply call the user model:
$user = User::query()->select('id', 'alternate_id', 'setujuterma', 'mykad', 'nama', 'email', 'notel', 'etunai',
'effective_rank','appointed_rank', 'akses', 'suspendreason', 'regstamp',
'matagajet', 'userrank','mykadverify','placement')
->with([
'penggunaRank' => function($q) use ($mid){
$q->select('id','ranktitle')->where('mid',$mid);
},
'appointedRankUser' => function($q) use ($mid){
$q->select('id','ranktitle')->where('mid',$mid);
},
'systemHirarki'=> function($q){
$q->select('id', 'nama');
},
'userHirarki' => function($q) use ($mid){
$q->select('hirarki','display')->where('mid',$mid);
},
'userPlacement' => function($q){
$q->select('id','alternate_id','nama');
}
])
->where('mid',$mid)
->where('akses','!=',-1);
if ($search) {
$user->where(function($q) use ($search){
$q->where("alternate_id","LIKE","%{$search}%")
->orWhere("nama","LIKE","%{$search}%")
->orWhere("mykad","LIKE","%{$search}%")
->orWhere("notel","LIKE","%{$search}%")
->orWhere("email","LIKE","%{$search}%");
});
}
return $user->orderBy('alternate_id','desc')
Hope everyone can get benefits from this. Thank you.
Please help;
i have models:
class Service extends \yii\db\ActiveRecord{
public function getCategory()
{
return $this->hasOne(ServiceCategory::className(), ['id' => 'service_category_id']);
}
}
class ServiceOffer extends \yii\db\ActiveRecord
{
public function getService()
{
return $this->hasOne(Service::className(), ['id' => 'service_id']);
}
public function getCategory()
{
return $this->hasOne(ServiceCategory::className(), ['id' => 'service_category_id'])->via('service');
}
public function getProperties()
{
return $this->hasMany(ServiceProperty::className(), ['service_category_id' => 'id'])
->via('category');
}
}
When i do query:
$query = ServiceOffer::find()->with(['properties'])->all();
or:
$query = ServiceOffer::find()->joinWith(['properties'])->all();
i have error:
Getting unknown property: app\models\ServiceOffer::service_category_id
but if i do this for see query sql:
$query = ServiceOffer::find()->joinWith(['properties']);
echo $query->prepare(\Yii::$app->db->queryBuilder)->createCommand()->rawSql;
sql:
SELECT "service_offer".* FROM "service_offer"
LEFT JOIN "service" ON "service_offer"."service_id" = "service"."id"
LEFT JOIN "service_category" ON "service"."service_category_id" = "service_category"."id"
LEFT JOIN "service_property" ON "service_category"."id" = "service_property"."service_category_id"
query without problem and execute
Why ActiveRecord find service_category_id property in ServiceOffer model?
I have table like below.
CREATE TABLE A (
id INT,
relationId INT,
status INT
)
CREATE TABLE B (
id INT,
status INT
)
The class file is like below
class A extends \yii\db\ActiveRecord {
public function getB() {
return $this->hasOne(B::class, ['id' => 'relationId']);
}
public function find() {
return new AQuery(__CLASS__);
}
}
class AQuery extends \yii\db\Query {
public function isActive() {
return $this->andWhere(['status' => 1]);
}
public function isNotActive() {
return $this->andWhere(['status' => 0]);
}
}
class B extends \yii\db\ActiveRecord {
public function find() {
return new BQuery(__CLASS__);
}
}
class BQuery extends \yii\db\Query {
public function isActive() {
return $this->andWhere(['status' => 1]);
}
public function isNotActive() {
return $this->andWhere(['status' => 0]);
}
}
I'm doing something like this
$model = A::find()
->joinWith([
'b' => function(BQuery $query) {
$query->isNotActive();
}
])
->isActive()
->one();
This will produce error
Column 'status' in where clause is ambiguous"
The only way I know is to manually add alias to $query->from and rewrite the $query->andWhere. But is there any easier way to reuse the query shorthand?
Use ActiveRecord::tableName() instead of aliasing (which doesn't seem to be an active record feature in Yii2). The tableName() can be accessed through the modelClass property of \yii\db\ActiveQuery.
public function isActive() {
$modelClass = $this->modelClass;
return $this->andWhere([$modelClass::tableName().'.status' => 1]);
}
You can enhance your isActive() method to accept aliases with an optionnal parameter. You can try something like this:
class AQuery extends \yii\db\Query {
protected function getAlias($alias = null) {
return $alias !== null ? $alias : A::tableName();
}
public function isActive($alias = null) {
$alias = $this->getAlias($alias);
return $this->andWhere(["{$alias}.status" => 1]);
}
public function isNotActive($alias = null) {
$alias = $this->getAlias($alias);
return $this->andWhere(["{$alias}.status" => 0]);
}
}
I want to list on a view something like
Number Commands | Date of commands | Description (text from table article)
All I have is
Number Commands | Date of commands | Description (id from table Bc)
I have two tables: BC (as commands) which have columns: id_commands, id_article, id_user, number_commands, date_commands, ...
and table Article which have columns : id_article, name_article, description_article, ...
According to this tutorial, How to extend the ZF2 skeleton application - entities with foreign keys
There are my codes:
On Bc\Model\Bc.php
class Bc implements BcInterface
{
...
public function setArticle(ArticleInterface $bc_article)
{
$this->article = $bc_article;
}
public function getArticle()
{
return $this->bc_article;
}
}
On Bc\Mapper\BcHydrator.php
namespace Bc\Mapper;
class BcHydrator // extends Zend\Stdlib\Hydrator\ClassMethods
{
public function extract($object)
{
return array(
'bc_id' => $object->getBc_id(),
'article_designation' => $object->getA_designation(),
'article_reference' => $object->getA_reference()
);
}
}
On Bc\Mapper\ZendDbSqlMapper.php
...
public function findAllWithArticle()
{
$sql = new Sql($this->dbAdapter);
$select = $sql -> select()
-> from( array('a'=>'s_article'))
-> join( array('bc' => 's_bc'),
'bc.bc_id_article = a.a_id');
$stmt = $sql->prepareStatementForSqlObject($select);
$result = $stmt->execute();
if ($result instanceof ResultInterface && $result->isQueryResult()) {
$resultSet = new HydratingResultSet($this->hydrator, $this->bcPrototype);
\Zend\Debug\Debug::dump($result);die();
//return $resultSet->initialize($result);
}
return array();
}
On my Controller
...
public function detailAction()
{
$id = $this->params()->fromRoute('id');
try {
//$bc = $this->bcService->findBc($id);
$bc = $this->bcService->findAllBcsWithArticle($id);
$articleDesignation = $bc->getArticle()->getA_designation();
} catch (\InvalidArgumentException $ex) {
return $this->redirect()->toRoute('bc');
}
return new ViewModel(array(
'bc' => $bc,
'article' => $articleDesignation
));
}
I have this error, when I have access to my view:
Fatal error: Call to undefined method
Zend\Db\ResultSet\HydratingResultSet::getArticle()
Did someone have an idea of what I did wrong?
Finally i found a solution for those who want to know.
I didn't use hydrator, but i have the result i want.
Add on my class Bc (Bc\Model\Bc.php) a protected variable $description and set setter and getter.
On my Mapper (Bc\Mapper\ZendDbSqlMapper.php), i added a function findContent ($id), and use as sql request:
$sql = new Sql($this->dbAdapter); $select = $sql -> select()
-> from( array('bc'=>'bc'))
-> join( array('a' => 'article'),
'bc.bc_id_article = a.id',
array(
'description' => 'description',
));
$select->where(array('bc_id = ?' => $id));
On my service (Bc\Service\BcService.php)
public function findBcsContent($id)
{
return $this->bcMapper->findContent($id);
}
And on my controller
public function indexAction()
{
return new ViewModel(array(
'bcs'=> $this->bcService->findAllBcsContent()
));
}
On my view, i could use
$this->escapeHtml($bc->getDesignation());
That's all
I have one tabel 'cms' when i call fetchAll() function which fetch all data fetch data with order by id desc means i want to pass orderby condition with fetchAll() function.
My Cms.php page is:
<?php
namespace Front\Model;
use Zend\Db\TableGateway\AbstractTableGateway;
class Cms extends AbstractTableGateway {
public function __construct($adapter) {
$this->table = 'cms';
$this->adapter = $adapter;
}
public function fetchAll($id) {
return $this->select();
}
public function getCmsContent($id){
$id = (int) $id;
$rowset = $this->select(array('id'=>$id));
if (!$row = $rowset->current()){
throw new \Exception ('Row not found');
}
return $row;
}
}
My Controller is :FrontController.php is:
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Front\Model\Cms;
use Front\Model\Setting;
use Front\Model\Slider;
class FrontController extends AbstractActionController
{
public function indexAction()
{
return array('cms_data'=>$this->getCms()->fetchAll('1'));
}
public function getCms(){
return $this->getServiceLocator()->get('Front\Model\Cms');
}
}
My Model is:
namespace Front;
class Module
{
public function getAutoloaderConfig()
{
return array('Zend\Loader\StandardAutoloader' =>
array('namespaces' =>
array(__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,),
),
);
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
// Add this method:
public function getServiceConfig()
{
return array(
'factories' => array(
'Front\Model\Cms' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$table = new \Front\Model\Cms($dbAdapter);
return $table;
},
),
);
}
}
?>
My View:index.php Page is:
<?php print_R($cms_data); ?>
An excerpt from the manual:
$select = new Select;
$select->order('id DESC'); // produces 'id' DESC
$select = new Select;
$select->order('id DESC')
->order('name ASC, age DESC'); // produces 'id' DESC, 'name' ASC, 'age' DESC
$select = new Select;
$select->order(array('name ASC', 'age DESC')); // produces 'name' ASC, 'age' DESC
The same method order applies to table gateway.