In the user table I have mobileno column for every user. And in the auth_assignment table I haveauthoriations. I want all mobile no of all the users who has autthoriation 'c_apo'. The output I want is only - 7777777777,9999999999.
The query I'm using is -
$mobiletemp = User::find()->leftJoin('auth_assignment', 'auth_assignment.user_id = user.id')->select('mobileno')->andWhere(['auth_assignment.item_name' => 'c_apo'])->asArray()->all();
$mobile = ArrayHelper::getColumn($mobiletemp, 'mobileno');
var_dump($mobile);
The output I'm getting is -
array(1) { [0]=> array(2) { [0]=> string(10) "9999999999" [1]=> string(10) "7777777777" } }
$mobile = User::find()
->select('mobileno')
->leftJoin('auth_assignment', 'auth_assignment.user_id = user.id')
->andWhere(['auth_assignment.item_name' => 'c_apo'])
->column();
$mobile = implode(",", $mobile);
//Here is a possible solution
public $exitingMember;//have to declare virtual field in model
$alreadyMember = TaskCrewEmployee::find()
->select('GROUP_CONCAT(tce_u_id) as exitingMember')
->where(['tce_ts_id'=>500])
->groupBy(['tce_ts_id'])
->one();
Related
I want to retrieve some data from 'tbl_karyawan' and input into 'tbl_absen' which is if the NIP exist from 'tbl_karyawan' then parshing some data into 'tbl_absen'. I was create the code, and the data is going well. but i have something trouble with
i want the data input in Nip_kyn be like 'KIP001' not [{"Nip_kyn":"KIP001"}].
this is my Model
public function presensi($data)
{
$isExist = DB::table('tbl_karyawan')
->where('Nip_kyn', $data)->exists();
if ($isExist) {
$namakyn = DB::table('tbl_karyawan')->where($data)->get('Nama_kyn');
$nippppp = DB::table('tbl_karyawan')->where($data)->select('Nip_kyn')->get($data);
$values = array('Nip_kyn' => $nippppp, 'Nama_kyn' => $namakyn, 'Jam_msk' => now(), 'Log_date' => today());
DB::table('tbl_absen')->insert($values);
} else {
echo 'data not available';
}
}
this is my controller
public function get()
{
$day = [
'time_in' => $this->AbsenModel->timeIN(),
'time_out' => $this->AbsenModel->timeOut(),
'break' => $this->AbsenModel->break(),
// absen here
's' => $this->AbsenModel->absensi(),
];
$data = [
'Nip_kyn' => Request()->Nip_kyn,
];
$this->AbsenModel->presensi($data);
return view('v_absen', $data, $day);
}
Yup, I finally got it, the problem is on my model.
$nama_karyawan = DB::table('tbl_karyawan')->where($data)->value('Nama_kyn');
$nipkyn = DB::table('tbl_karyawan')->where($data)->value('Nip_kyn');
I just change 'get' to 'value'.
I am new to cakephp.Below the number being bold is the time the data is being created and code to validate .My problem is to validate the data should between 8 hours not more that using Model. is there any wrong in my code?
sample data=L02A-180129-1215-A
The code to find the table based on data
public function sa01() {
$trv_no = $this->data[$this->alias]['TRV_No_01'];
$line_no = intval(substr($trv_no, 1, 2));
$table_name = 'Ticket_L' . $line_no;
$this->Ticket->setSource($table_name);
$this->Ticket->recursive =-1;
code for validation
Batch_time is a column name for the table
$time = $this->Ticket->find('all',array('conditions' => array('Ticket.Batch_Time >=' => date('Y-m-d H:i:s', strtotime('-8 hour')))));
if(empty($time))
{
$table_name = 'Ticket_L0';
$this->Ticket->setSource($table_name);
//$ticket = $this->Ticket->find('first', array('conditions' => array('Ticket.TRV_No' => $trv_no)));
$time = $this->Ticket->find('all',array('conditions' => array('Ticket.Batch_Time >=' => date('Y-m-d H:i:s', strtotime('-8 hour')))));
if(empty($time)) { return false; } else { return true; }
}
else
{ return true; }
}
I just recently noticed that my int's and boolean's and all else are coming out of the DB as strings. Is there any means of which I can tell the DB helper to stop that behavior and give me the actual type from the DB rather than casting it all as strings?
I've done the following and works as you demand:
table users
id: int
name: varchar(256)
// Model: users_model
function get_users() {
$sql = 'SELECT id, name
FROM users';
$query = $this->db->query($sql);
return $query->result_array();
}
// Controller
function test() {
$this->load->model('users_model');
$users = $this->users_model->get_users();
var_dump( $users[0] );
}
And this is the result
array(2) { ["id"]=> int(87) ["name"]=> string(4) "John" }
I need to get result from my database as associative array. I have gardens and families that related to this gardens as (One to Many). So for each garden i need to get array of families. So garden must be key, and array of families - it is a values. So i tried:
public function getFamiliesInGardens($client)
{
$qb = $this->createQueryBuilder('g');
$qb
->select(['g.name as garden', 'family.name', 'family.id'])
->join('g.growing', 'growing')
->join('growing.plant', 'plant')
->join('plant.family', 'family')
->where('g.client = :client')
->orderBy('g.name')
->addOrderBy('growing.endDate', 'DESC')
->setParameter('client', $client);
$grow = $qb->getQuery()->getArrayResult();
return $grow;
}
I got:
[0] => array(3) {
["garden"]=> string(1) "1"
["name"]=>string(9) "Brassicas"
["id"]=> int(13)
}
[1] =>
array(3) {
["garden"]=> string(1) "1"
["name"]=> string(13) "Miscellaneous"
["id"]=> int(18)
}
But i expect:
[0] => array(1) {
["1"] => array(2) {
[0] => array(2) {
["name"] =>string(9) "Brassicas"
["id"] => int(13)
},
[1] => array(2) {
["name"]=>string(9) "Miscellaneous"
["id"]=> int(18)
},
}
}
If i will add group by garden, result will be the same as first, but garden will be chosed once without repeat. Same for family. So, what can i change to get array: garden => families?
I came across this use case 3 years ago where I hoped the same. However, GROUP BY does not work that way.
You would have to manually iterate over the records and group them as you require. For example, something like this:
$gardens = ....
$groups = [];
for ( $gardens as $g ){
if (!array_key_exists($g['id'], $groups)){
$groups[$g['id']] = [];
}
$groups[$g['id']][] = $g;
}
I am using the following code in my _form file for yii\jui\DatePicker, on date selection it is showing the date format properly. In gridview the data rendered is OK.
But when I open the record for update, the format shown is yyyy-mm-dd, I want to show the same as dd-mm-yyyy
<?= $form->field($model, 'birth_date')->widget(DatePicker::className(),
[
'language' => 'en',
'clientOptions' =>[
'dateFormat' => 'd-m-yy',
'language' => 'US',
'country' => 'IN',
'showAnim'=>'fold',
'yearRange' => 'c-25:c+0',
'changeMonth'=> true,
'changeYear'=> true,
'autoSize'=>true,
'showOn'=> "button",
//'buttonImage'=> "images/calendar.gif",
'htmlOptions'=>[
'style'=>'width:80px;',
'font-weight'=>'x-small',
],]]) ?>
Thanks.
You can format date in your controller, then render the view. In controller:
$model->birth_date=Yii::$app->formatter->asDate($model->birth_date, "dd-mm-yyyy");
Take a look at Yii2's official formatter guide here.
You can Generalize the date format
class Setup {
const DATE_FORMAT = 'php:Y-m-d';
const DATETIME_FORMAT = 'php:Y-m-d H:i:s';
const TIME_FORMAT = 'php:H:i:s';
public static function convert($dateStr, $type='date', $format = null) {
if ($type === 'datetime') {
$fmt = ($format == null) ? self::DATETIME_FORMAT : $format;
}
elseif ($type === 'time') {
$fmt = ($format == null) ? self::TIME_FORMAT : $format;
}
else {
$fmt = ($format == null) ? self::DATE_FORMAT : $format;
}
return \Yii::$app->formatter->asDate($dateStr, $fmt);
}
}
Then anywhere else (like controller/model) you can access this function to convert any input date/time string for saving to database.
$model->birth_date = Setup::convert($model->dateAttr);
$model->birth_date = Setup::convert($model->datetimeAttr, 'datetime');