CakePhp AngularJs pagination - angularjs

I started learning CakePhp and AngularJs, I want to get pagination working.
I found this simple tutorial: http://florian-kraemer.net/2015/10/cakephp-angularjs-pagination/ but I can't get it to work.
This is the code from the example:
public function index() {
$query = $this->Table->find();
if (empty($this->request->params['paging'][$this->Table->alias()])) {
$paging = false;
} else {
$paging = $this->request->params['paging'][$this->Table->alias()];
}
$this->set('records', $this->paginate($query));
$this->set('paging', $paging);
$this->set('_serialize', ['records', 'paging']);
}
My Model is called Entry, how to I have to change the code for this example to work, I tried it like this but it's not working:
public function index() {
$query = $this->Entry->find();
if (empty($this->request->params['paging'][$this->Entry->alias()])) {
$paging = false;
} else {
$paging = $this->request->params['paging'][$this->Entry->alias()];
}
$this->set('records', $this->paginate($query));
$this->set('paging', $paging);
$this->set('_serialize', ['records', 'paging']);
}
I am not sure what to change in my code to get it working. Any help would be appreciated.
Thanks, Gregor

Please try this code, it should be work. You have to declare $paging after paginate.
public function index() {
$query = $this->Entry->find();
$this->paginate($query);
if (empty($this->request->params['paging'][$this->Entry->alias()])) {
$paging = false;
} else {
$paging = $this->request->params['paging'][$this->Entry->alias()];
}
$this->set(compact('records', 'paging'));
$this->set('_serialize', ['records', 'paging']);
}

Related

How to register afterMarshal in CakePHP 4.1

I noticed that there is new afterMarshal event in 4.1.
Where to put it? In Table model? And how?
I want to do some work with results every time it's loaded.
Thanks for help
For Encryption and Decryption through model in 'CAKEPHP 4'
public $encryptedFields = ['first_name','last_name'];
public function beforeSave($event, $entity, $options)
{
foreach($this->encryptedFields as $fieldName)
{ if($entity->has($fieldName))
{ $entity->set($fieldName, encodeBeforeSave($entity->get($fieldName)));}
} return true;
}
public function beforeFind( $event, $query, $options)
{ $query->formatResults(
function ($results)
{ return $results->map(function ($row){
foreach($this->encryptedFields as $fieldName)
{
if(isset($row[$fieldName]) && !empty($row[$fieldName]) )
{
$row[$fieldName] = decodeBeforefind($row[$fieldName]);
}
}
return $row;
});
}
);
}

Function Download in Yii2

public function actionUnduh($id) {
$download = PstkIdentifikasi::findOne($id);
$path = Yii::getAlias('../web/bukti/') . $download->bukti;
if (file_exists($path)) {
//return \Yii::$app->response->sendFile($download->pre_paper,#file_get_contents($path));
return Yii::$app->response->sendFile($path);
}
}
I need to download file from folder web/bukti, the code not error but the code doesn't work, Anyone can help me :(
public function actionUnduh($id)
{
$download = PstkIdentifikasi::findOne($id);
$path = Yii::getAlias('#webroot').'/bukti/'.$download->bukti;
if (file_exists($path)) {
return Yii::$app->response->sendFile($path, 'File name here');
}
}
Refer below:
Yii2 Aliases
Yii2 sendFile()
Firstly you can write an action in SiteController.php like this:
public function actionDownload()
{
$file=Yii::$app->request->get('file');
$path=Yii::$app->request->get('path');
$root=Yii::getAlias('#webroot').$path.$file;
if (file_exists($root)) {
return Yii::$app->response->sendFile($root);
} else {
throw new \yii\web\NotFoundHttpException("{$file} is not found!");
}
}
then you can call this function anywhere:
Yii::$app->urlManager->createUrl(['site/download','path'=>'/upload/files/','file'=>'filename.pdf'])
Be careful your files must be in this directory:
"backend/web/upload/files/filename.pdf"
or
"frontend/web/upload/files/filename.pdf"

How to check model column exist in cakephp2 or not?

public function beforSave($option = array()) {
if($this->columnName) {
// Statement
}
}
Something like that
if(!$this->loadModel($type)) {
// Statement
}
Get the model schema
$this->modelName->schema();
Check if my field is available or not
if (!empty($this->modelName->schema('date_created'))) {
// statement
}
Or
public function beforeSave($options = array()) {
if ($this->hasField('date_created')) {
$this->data[$this->alias]['date_created'] = date('Y-m-d H:i:s');
}
}
Try with getColumnType and array_key_exist;
getColumnTypes() Returns an associative array of field names and column
types.
$fileds = $this->YourModelName->getColumnTypes();
if(array_key_exists('columnName', $fileds)) {
// Statement
}

Codeigniter - Array dont work correctly

Whenever I call this function, I get the user_id correctly but the password isnt checked...
Model:
<?php
class Prometheus_model extends CI_Model {
var $tables = array(
'bots' => 'bots',
'users' => 'users'
);
function __construct() {
parent::__construct();
}
public function tablename($table = NULL) {
if(! isset($table)) return FALSE;
return $this->tables[$table];
}
public function get($table, $where = array(), $order = NULL) {
$this->db->where($where);
if(isset($order)) {
$this->db->order_by($order);
}
$q = $this->db->get_where($this->tablename($table),$where);
$result = $q->result_array();
// You should use $q->num_rows() to detect the number of returned rows
if($q->num_rows()) {
return $result[0];
}
return $result;
}
public function update($table, $where = array(), $data) {
$this->db->update($this->tablename($table),$data,$where);
return $this->db->affected_rows();
}
public function insert($table, $data) {
$this->db->insert($this->tablename($table),$data);
return $this->db->insert_id();
}
public function delete($table, $where = array()) {
$this->db->delete($this->tablename($table),$where);
return $this->db->affected_rows();
}
public function explicit($query) {
$q = $this->db->query($query);
if(is_object($q)) {
return $q->result_array();
} else {
return $q;
}
}
public function num_rows($table, $where = NULL) {
if(isset($where)){
$this->db->where($where);
}
$q = $this->db->get($table);
return $q->num_rows();
}
public function get_bot_data_by_hw_id($bot_hw_id) {
$q = $this->get('bots', array('bot_hw_id' => $bot_hw_id));
return $q;
}
public function check_user_data($user_incredials, $user_password) {
if($this->num_rows('users', array('user_name' => $user_incredials, 'user_password' => $this->encrypt->decode($user_password))) == 1){
$q = $this->get('users', array('user_name' => $this->security->xss_clean($user_incredials)));
return $q['user_id'];
}
return FALSE;
}
}
?>
My function-calling at the controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller {
public function index(){
if($this->input->post('user_login')){
var_dump($this->prometheus_model->check_user_data($this->input->post('user_incredials'), $this->input->post('user_password')));
}
$this->load->view('login_index');
}
}
How can i fixx this ?
In your check_user_data() method you are using
if($this->num_rows('users', array('user_name' => $user_incredials, 'user_password' => $this->encrypt->decode($user_password))) == 1)
I think (logically) following code
$this->encrypt->decode($user_password)
should be
$this->encrypt->encode($user_password)
because, you are calling num_rows() method and it is
public function num_rows($table, $where = NULL)
{
if(isset($where)){
$this->db->where($where);
}
$q = $this->db->get($table);
return $q->num_rows();
}
which is actually querying the data base something like, for example,
select * from USERS where user_name = 'heera' and password = decode('abcde12345')
In this case, the password you are trying to match is need to be encrypted using encode (not decode) method, because the user has given you a non-encrypted (plain) password and the password saved in the database is already encrypted, so encode the plain password using encode method before you query the database to match with already encoded passwords.

registration and login in cakephp2.0

Good day! I am now migrating my codes from 1.3 to 2.0 of CakePHP. And I just want to ask, how can I do this code (from 1.3) to 2.0? Here is the code:
function register() {
if(!empty($this->data)) {
// unset unrequired validation rules
unset($this->User->validate['username']['check_user']);
// validate & save data
if($this->User->save($this->data)) {
$this->data['User']['Password'] = md5($this->data['User']['Password']);
$this->User->save($this->data);
// set Flash & redirect
$this->Session->setFlash('You have successfully registered.','default',array('class'=>'flash_good'));
$this->redirect(array('action'=>'login'));
}
else{
//$this->Session->setFlash(__('The user could not be saved.' , true));
//$this->redirect(array('action' => 'register'));
}
}
}
and here is my attempt code that I tried to resolve:
public function register() {
if($this->request->is('post')) {
//unset($this->User->validate['username']['check_user']);
// validate & save data
//$this->data['User']['Password'] = md5($this->data['User']['Password']);
$this->request->data('User.Password', $this->request->data('User.Password'));
// $this->User->save($this->data);
// set Flash & redirect
if($this->User->save($this->request->data)) {
$this->Session->setFlash('You have successfully registered.','default',array('class'=>'flash_good'));
$this->redirect(array('action'=>'login'));
}
else{
//$this->Session->setFlash(__('The user could not be saved.' , true));
//$this->redirect(array('action' => 'register'));
}
}
}
This code is for the login, made in 1.3
function login() {
//echo $_SESSION['User']['auth'];
if(!isset($_SESSION['User']['id'])){
if(!empty($this->data)) {
if(($user = $this->User->validateLogin($this->data['User'])) == true)
{
//print_r(md5($this->data['User']['password']));
$user = $this->User->find('first',array('conditions'=>array('Username'=>$this->data['User']['Username'],'Password'=>md5($this->data['User']['Password']))));
//print_r ($user);
if(!empty($user)){
$_SESSION['User']['id'] = $user['User']['id'];
$_SESSION['User']['name'] = $user['User']['Name'];
$_SESSION['User']['auth'] = $user['User']['auth'];
$this->redirect(array('controller'=>'ads','action'=>'index'));
}else{
$this->Session->setFlash('Username/Password not match');
$this->redirect(array('action'=>'login'));
}
}
}
}
else{
$this->Session->setFlash('Login First.');
$this->redirect(array('controller'=>'ads','action'=>'index'));
}
}
and here is my code in 2.0 and still it is not working also.
public function login() {
if(!($this->Session->read('user_id'))){
if($this->request->is('post')) {
//$user = $this->User->find('first',array('conditions'=>array('Username'=>$this->data['User']['Username'],'Password'=>md5($this->data['User']['Password']))));
if(!empty($user)){
$this->Session->write('user_id',$user['User']['id']);
$this->Session->write('name',$user['User']['Name']);
//$this->Session->write('name',$user['User']['Name']);
$this->redirect(array('controller'=>'ads','action'=>'index'));
}else{
$this->Session->setFlash('Username/Password not match');
$this->redirect(array('action'=>'login'));
}
}
}else{
$this->redirect(array('controller'=>'ads','action'=>'index'));
}
}//end login
I hope that someone respond to my question. Thanks in advance.
I would first try running the migration on your existing code and you might be surprised. here is the link to the upgrade shell:
http://book.cakephp.org/2.0/en/console-and-shells/upgrade-shell.html#upgrade-shell
Try that first.

Resources