Guys how can I apply an OR condition in the following Where statement ?
$this->IbCommisions->find()->matching(
'Users.Accounts',function ($q) {
return $q->where(['IbCommisions.ib_id' => $this->userid, 'Users.country_id' => $this->request->data['country']]);
}
To become something like
(['IbCommisions.ib_id' => $this->userid OR 'Users.country_id' => $this->request->data['country']])
return $q->where(['OR' => [
'IbCommisions.ib_id' => $this->userid,
'Users.country_id' => $this->request->data['country']
]]);
or alternatively
return $q->where(['IbCommisions.ib_id' => $this->userid])
->orWhere(['Users.country_id' => $this->request->data['country']]);
http://book.cakephp.org/3.0/en/orm/query-builder.html#advanced-conditions
Related
Below is the function I used to update my record.
public function store_upd_nilai_uji_program(Request $request)
{
$validated = $request->validate([
'nip' => 'required|numeric|digits_between:18,18',
'n1' => 'nullable|numeric',
'n2' => 'nullable|numeric',
'n3' => 'nullable|numeric',
'n4' => 'nullable|numeric',
'n5' => 'nullable|numeric',
'n6' => 'required|numeric',
'tanggal' => 'required',
'waktu' => 'required',
]);
NilaiUjiProgram::where('nim', $request->nim)->update([
'nip' => $request->nip,
'nilai_kemampuan_dasar_program' => floatval($request->n1),
'nilai_kecocokan_algoritma' => floatval($request->n2),
'nilai_penguasaan_program' => floatval($request->n3),
'nilai_penguasaan_ui' => floatval($request->n4),
'nilai_validasi_output' => floatval($request->n5),
'total' => floatval($request->n6),
'tanggal' => $request->tanggal,
'waktu' => $request->waktu,
]);
return redirect('/prodi/daftar_nilai_uji_program')->with('status', 'Nilai berhasil diperbaharui!');
}
Actually, I want to update the attribute 'nilai_kemampuan_dasar_program', 'nilai_kecocokan_algoritma','nilai_penguasaan_program','nilai_penguasaan_ui', and 'nilai_validasi_output' if only the equal value in $request is NOT NULL. If the value is null, I would like to keep my record fill with NULL. To keep in mind that my table doesn't have any primary key. Do you have any idea how to solve my problem?
NilaiUjiProgram::where('nim', $request->nim)->update([
'nip' => $request->nip,
'nilai_kemampuan_dasar_program' => $request->n1 ? floatval($request->n1) : null,
'nilai_kecocokan_algoritma' => $request->n2? floatval($request->n2) :null,
'nilai_penguasaan_program' => $request->n3 ? floatval($request->n3):null,
'nilai_penguasaan_ui' => $request->n4 ? floatval($request->n4) : null,
'nilai_validasi_output' => floatval($request->n5),
'total' => floatval($request->n6),
'tanggal' => $request->tanggal,
'waktu' => $request->waktu,
]);
I have used Left Join Query in file called "directoryDataTable.php. Now the problem is that Yajra DataTable search bar is not working. Its neither giving any error nor the search result.
My DataTable query function is al follows.
public function query()
{
$id = \Illuminate\Support\Facades\Auth::user()->id;
$directories = DB::table('directories')
->leftjoin('claimed', 'directories.id', '=','claimed.dir_id')
->select('directories.*')
->where('directories.user_id',$id)
->where('paymentStatus','1')
->whereNull('directories.deleted_at')
->orWhere('claimed.claimed_by',$id);
return $this->applyScopes($directories);
}
Please Help
Changed the query to,
public function query()
{
$id = \Illuminate\Support\Facades\Auth::user()->id;
$directories = DB::table('directories')
->leftjoin('claimed', 'directories.id', '=','claimed.dir_id')
->select('directories.*')
->where(function ($query) {
$query->where('directories.user_id',\Illuminate\Support\Facades\Auth::user()->id)
->orWhere('claimed.claimed_by',\Illuminate\Support\Facades\Auth::user()->id);
})
->where('paymentStatus','1')
->whereNull('directories.deleted_at')
;
return $this->applyScopes($directories);
}
and in get columns function, replace ['name' => 'YourTableName.ColumnName','data' => 'YourColumnName'] Like this.
private function getColumns()
{
return [
'dir_name' => ['name' => 'directories.dir_name', 'data' => 'dir_name'],
'phone_number' => ['name' => 'directories.phone_number', 'data' => 'phone_number'],
'address' => ['name' => 'directories.address', 'data' => 'address'],
'features' => ['name' => 'directories.features', 'data' => 'features', ],
'Status' => ['name' => 'directories.Status', 'data' => 'Status','searchable'=>false ],
'Subscription' => ['name' => 'directories.Subscription', 'data' => 'Subscription','searchable'=>false ]
];
}
I have this problem: I need to get data from database and filter them. but then I need to use custom php function to filter those filtered results using data from it.
Clasic search function in ActiveDataProvider
public function search($params) {
$query = Passenger::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
// I guess my function would go like here
Passenger::filterResultsEvenMore($dataProvider);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'passenger_id' => $this->passenger_id,
// ...
'version' => $this->version,
'status' => $this->status,
]);
return $dataProvider;
}
So my question is how to work with results of dataProvider because if I vardump the variable it looks like this and no actual data there.
yii\data\ActiveDataProvider Object
(
[query] => common\models\PassengerQuery Object
(
[sql] =>
[on] =>
[joinWith] =>
[select] =>
[selectOption] =>
[distinct] =>
[from] =>
[groupBy] =>
[join] =>
[having] =>
[union] =>
[params] => Array()
[_events:yii\base\Component:private] => Array()
[_behaviors:yii\base\Component:private] => Array()
[where] => Array
(
[status] => 1
)
[limit] =>
[offset] =>
[orderBy] =>
[indexBy] =>
[emulateExecution] =>
[modelClass] => common\models\Passenger
[with] =>
[asArray] =>
[multiple] =>
[primaryModel] =>
[link] =>
[via] =>
[inverseOf] =>
)
[key] =>
[db] =>
[id] =>
[_sort:yii\data\BaseDataProvider:private] =>
[_pagination:yii\data\BaseDataProvider:private] =>
[_keys:yii\data\BaseDataProvider:private] =>
[_models:yii\data\BaseDataProvider:private] =>
[_totalCount:yii\data\BaseDataProvider:private] =>
[_events:yii\base\Component:private] => Array()
[_behaviors:yii\base\Component:private] =>
)
UPDATE
I need to use function like this for each record:
if (myFunction(table_column_1, table_column_2)) {
result_is_ok_return_it
} else {
do_not_return_this_record
}
Why do you don't add your additional filters to query object used in DataProvider?
You can parse your conditions to $query->andFilterWhere(). If you need custom function for it just modify $dataProvider->query object inside function. After execute query in data provider you can only filter results by manually filter array of models stored in $dataProvider->models
To get result use models property or getModels()
For example,
$dataProvider->models;
OR
$dataProvider->getModels();
I think I came across a solution, (looks like it is working)
http://www.yiiframework.com/doc-2.0/yii-data-basedataprovider.html#setModels()-detail
After I do all my usual search stuff as described in question at beginning, I would do something like this using setModels() function
class PassengerSearch extends Passenger
public $status; // virtual attribute not present in database table
public function rules()
{
return [
// ... some other rules
[['status'], 'safe'],
];
}
// ...
$filtered_models = [];
$filter_models = false; // if you only want to filter if there is some value
foreach ($dataProvider->models as $model) {
// if ($model->status == 1) // example
if (!empty($this->status) && $model->status == $this->status) { // better approach, using virtual attribute $status
$filter_models = true;
$filtered_models[] = $model;
}
}
if ($filter_models)
$dataProvider->setModels($filtered_models);
return $dataProvider;
}
I want to get a single row from db, remove one element from row and update that row back to db.
I wrote this code
$query = $this->db->get_where(TABLENAME, array('pin_code' => $pincode), 1);
$result = $query->result_array();
$array = $result[0];
if (($key = array_search($ccode, $array)) !== false) {
unset($array[$key]);
} //$code is the value which i want to remove
$this->db->where('pin_code', $pincode);
$this->db->update(TABLENAME, $array);
This is row
Array ( [pin_code] => 854101 [ccode_1] => 5806 [ccode_2] => [ccode_3] => [ccode_4] => [ccode_5] => [ccode_6] => [ccode_7] => [ccode_8] => [ccode_9] => [ccode_10] => [ccode_11] => [ccode_12] => [ccode_13] => [ccode_14] => [ccode_15] => [ccode_16] => [ccode_17] => [ccode_18] => [ccode_19] => [ccode_20] => [ccode_21] => [ccode_22] => [ccode_23] => [ccode_24] => [ccode_25] => [ccode_26] => [ccode_27] => [ccode_28] => [ccode_29] => [ccode_30] => )
i just want to remove ccode_1(this is not fixed depends upon $ccode value) value and update it back to db.
but its not showing desired result as above.
You don't need to unset the desired index just get that index and apply update
if (($key = array_search($ccode, $array)) !== false) {
$this->db->where('pin_code', $pincode);
$this->db->update(TABLENAME, array($key => ''));
}
Product.find(:all,
:conditions => { :companies => { :malls => { :id => 1},:products => {:title => "nexus one"} }},
:joins => [:company => :mall])
I found the solution, I think I have to take break,
Product.find(:all,:conditions => ['title LIKE ? and mall_id = ?', "%#{search}%",mall_id],
:joins => [:company => :mall])