I am trying to implement memcache into my application, but I am running into some strange issues. If I run find() when there is no cache, then I get no results found. However, if I run the same query (refresh the page), then I get the results I expect.
Here is my code:
<?php
//Set the models cache service
$di->set('modelsCache', function(){
// Cache data for one day by default
$frontCache = new \Phalcon\Cache\Frontend\Data(array(
"lifetime" => 86400
));
// Memcached connection settings
$cache = new \Phalcon\Cache\Backend\Memcache($frontCache, array(
"host" => "localhost",
"port" => "11211"
));
return $cache;
});
<?php
class VideosController extends IndexController {
public function viewAction() {
/**
* This will always return an array;
* param 0 will always be the int
*/
$params = $this->dispatcher->getParams();
$id = $params[0];
$cacheName = 'videoForView_'.$id;
try {
$video = Videos::findFirst(array(
'id='.$id,
'cache' => array(
'key' => $this->router->getControllerName().'_'.$this->router->getActionName().'_'.$id
)
));
$this->view->setVar('video', $video);
} catch(Exception $e) {
echo '<pre>';
var_dump($e);
exit;
}
}
}
I am under the assumption that the Phalcon Model class would return me a database query, if there is no hit in the memcache server. It seems that the find() does find something and caches it, but it does not return me anything. Am I not understanding how the caching works in Phalcon? Or am I just doing something wrong?
The issue has been resolved. I worked with Andres (one of the developers of Phalcon), helping him resolve the issues I was having. Turns out it was deeper in the code than I would've expected.
The good news is it's resolved, and pushed up to the 0.8.0 branch on phalcon's github! Don't forget to recompile!
Related
I am learning Slim Framework v4 and decided to use Respect\Validation to validate inputted data and have hit a snag where I do not know how to inject the PDO into my custom rule I created.
The idea is to validate some inputs against the database if the provided data exist (or in another instances, if it was inputted correctly). In this specific case, I am tying to validate user's credentials for log in. My idea is this:
AuthController.php:
v::with('app\\Validators\\');
$userValidation = v::notBlank()->email()->length(null, 255)->EmailExists()->setName('email');
EmailExists() is my custom rule.
EmailExists.php:
namespace app\Validators;
use PDO;
use Respect\Validation\Rules\AbstractRule;
class EmailExists extends AbstractRule
{
protected $pdo;
public function __construct(PDO $pdo)
{
$this->pdo = $pdo;
}
public function validate($input, $id = null)
{
// a PDO query that checks if the email exists in database
}
}
But I get an error of Too few arguments to function app\Validators\EmailExists::__construct(), 0 passed and exactly 1 expected, which is somewhat expected since the AbstractRule does not have a PDO injected and my class extends it.
So how to inject the PDO interface so that I can use it in my custom rules?
Are you guys using another approach in validating this kind of data? Do note that I am writing an API, so the database validation is somewhat a must and after Googling for past two days, I have no solutions at hand.
I am also using a PHP-DI where I create PDO interface. This is my dependencies.php file:
declare(strict_types=1);
use DI\ContainerBuilder;
use Psr\Container\ContainerInterface;
use app\Handlers\SessionMiddleware;
return function (ContainerBuilder $containerBuilder) {
$containerBuilder->addDefinitions([
PDO::class => function (ContainerInterface $c) {
$settings = $c->get('settings')['db'];
$db = new PDO("mysql:host={$settings['host']};dbname={$settings['database']};charset={$settings['charset']},{$settings['username']},{$settings['password']}");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8',time_zone='{$offset}'");
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
return $db;
},
'session' => function(ContainerInterface $c) {
return new SessionMiddleware;
}
]);
};
And (part of) index.php:
declare(strict_types=1);
use DI\ContainerBuilder;
use Slim\Factory\AppFactory;
// Instantiate PHP-DI ContainerBuilder
$containerBuilder = new ContainerBuilder();
// Set up settings
$settings = require __DIR__ . '/../app/settings.php';
$settings($containerBuilder);
// Set up dependencies
$dependencies = require __DIR__ . '/../app/dependencies.php';
$dependencies($containerBuilder);
// Build PHP-DI Container instance
$container = $containerBuilder->build();
// Instantiate the app
AppFactory::setContainer($container);
$app = AppFactory::create();
// Register middleware
$middleware = require __DIR__ . '/../app/middleware.php';
$middleware($app);
// Register routes
$routes = require __DIR__ . '/../app/routes.php';
$routes($app);
// Add Routing Middleware
$app->addRoutingMiddleware();
// Run App & Emit Response
$response = $app->handle($request);
$responseEmitter = new ResponseEmitter();
$responseEmitter->emit($response);
Any help would be appreciated.
Use your user model to count the number of rows in the user table where there is a hit.
If it is not exactly 0, the check returns false, if it is exactly 0, the check passes.
So you don't have to include a PDO at this point. I use Slim 3 and that works quite well.
namespace app\Validators;
use Respect\Validation\Rules\AbstractRule;
class EmailAvailable extends AbstractRule {
/**
* #param $input
*
* #return bool
*/
public function validate ($sInput) {
return User::where('user_email', $sInput)->count() === 0;
}
}
class EmailAvailable extends AbstractRule {
/**
* #param $input
*
* #return bool
*/
public function validate ($sInput) {
return User::where('user_email', $sInput)->count() === 0;
}
}
I am having an issue inserting a record into the database. I am a beginner with the Yii framework, so I may have made some stupid mistakes.
This is from the SiteController
public function actionCreatePost(){
$model = new PostForm();
$post = new Post();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$post->body = $model->body;
$post->title = $model->title;
$post->save();
return $this->redirect('index');
}else {
return $this->render('createPost', ['model' => $model]);
}
}
This is from the Post class
public function behaviors()
{
return [
[
'class' => TimestampBehavior::className(),
'createdAtAttribute' => 'created_at',
'updatedAtAttribute' => 'updated_at',
'value' => new Expression('NOW()'),
],
[
'class' => BlameableBehavior::className(),
'createdByAttribute' => 'id_author',
]
];
}
The issue is that you have created a PostForm class for the form (which is correct) but you are then trying to load the response into the Post class - a completely different class. This won’t work without modification.
If you have a look at the response:
var_dump(Yii:$app->request->post());
You will see the form data is located within the PostForm key. Yii will therefore only load the data into the PostForm class.
The correct solution is therefore to create a savePost() function within the PostForm eg:
public function savePost(){
$model = new Post();
$model->propertyABC = $this->propertyABC
...etc...
$model->save();
So the action would appear as follows:
$model = new PostForm();
If($model->load(Yii::$app->request->post()) && $model->validate()){
$model->savePost();
The other option is to rename the key from PostForm to Post. Yii will then load the data but this is not the best approach as it is a bit obscure.
Hope that helps
I would guess the issue is with the validation.
I can see several issues I will point out. First, I cannot figure out why are you creating a new PostForm, loading the data in it and verifying it, just to dump some values in a new Post and save it. Are there some functions, you are running in the PostForm model, that are triggered by load or verify? If that is not the case, I would suggest dropping one of the models, and using only the other. Usually, that is the Form model. It serves as a link between the ActiveForm and the model handling everything. You can do everything in the createPost() function in the Form model, and then in the controller it will look like
if ($model->load(Yii::$app->request->post())) {
$model->save();
return $this->redirect('index');
}
Second of all, you can dump post->getErrors() before the save to see if there are any errors with the validation. What you can also do, is call $post->save(false) instead. If you pass false to it, it will not trigger $post->validate(), and some errors can be neglected. Please, let me know if there is anything unclear.
In my controller I throw a 404 response after an If statement, something like that :
if ($foo) {
$this->getResponse()->setStatusCode(404);
return;
}
Then, I would like to send some variables to my 404 page. In my mind, I want to do something like that :
$this->getResponse()->setVariables(array('foo' => 'bar', 'baz' => 'bop'));
$this->getResponse()->setStatusCode(404);
return;
It's not the good solution, so how I have to do that ?
And after, how to get these variables in my 404 view ?
Thank you
Oh god..
I was so dumb
Solution :
if ($foo) {
$this->getResponse()->setStatusCode(404);
return array('myvar' => 'test');
}
In 404.phtml :
<?php echo $this->myvar; ?>
I've come to this question from Google and my issue was a bit more difficult. Since 404 error could be thrown from absolutely unpredictable url, you can't be sure you catched it in some controller. Controller – is too late to catch 404 error.
The solution in my case was to catch an EVENT_DISPATCH_ERROR and totally rebuild viewModel. Cavern is that layout – is a root viewModel, and content appended into layout by default is another viewModel (child). These points are not such clear described in official docs.
Here is how it can look like in your Module.php:
public function onBootstrap(MvcEvent $event)
{
$app = $event->getParam( 'application' );
$eventManager = $app->getEventManager();
/** attach Front layout for 404 errors */
$eventManager->attach( MvcEvent::EVENT_DISPATCH_ERROR, function( MvcEvent $event ){
/** here you can retrieve anything from your serviceManager */
$serviceManager = $event->getApplication()->getServiceManager();
$someVar = $serviceManager->get( 'Some\Factory' )->getSomeValue();
/** here you redefine layout used to publish an error */
$layout = $serviceManager->get( 'viewManager' )->getViewModel();
$layout->setTemplate( 'layout/start' );
/** here you redefine template used to the error exactly and pass custom variable into ViewModel */
$viewModel = $event->getResult();
$viewModel->setVariables( array( 'someVar' => $someVar ) )
->setTemplate( 'error/404' );
});
}
New to kohana here. I have a task from my internship to make login system with kohana framework 3.2 . I also did it to insert,update and delete stuff with auto modeler ORM. I have some trouble now with kohana auth. I already have the database structure Imported and inserted an user in the 'users' table and give him a role in the 'roles_user' table.
Also created an Auth.php file in APP/config/ :
return array(
'driver' => 'AutoModeler_ORM',
'hash_method' => 'sha256',
'hash_key' => 'Somebiglonghaskeyofmixedcharacters102345567709',
'lifetime' => 1209600,
'session_type' => Session::$default,
'session_key' => 'auth_user',
);
In my controller , I have a login function with the following code:
if ($_POST)
{
$post = $this->request->post();
$success = Auth::instance()->login($post['email'], $post['password']);
if ($success)
{
echo "Welcome!";
}
else
{
echo "Something goes wrong...";
}
}
I already have activated the modules in the bootstrap.
pastebin link to my role model : http://pastebin.com/bQYReETh
pastebin link to my user model : http://pastebin.com/ufzvKjmA
The problem is that I always come in the else.
Does somebody have an idea whats going on?
Do I miss something?
#Woodle,
Maybe adding a _constructor can help.
public function __construct($id = NULL)
{
if ($id !== NULL)
{
$this->load(db::select_array($this->fields())->where($this->_table_name.'.username', '=', $id));
}
elseif ($this->id) // We loaded this via mysql_result_object
{
parent::__construct($id);
}
}
I'm trying to load, edit and save a record with CakePHP 2.0 but I get a generic error during the save method that don't help me to understand where is the problem.
if I try with debug($this->User->invalidFields()); I get an empty array, but I get false from $this->User->save() condition.
Here is the controller action where I get the error:
public function activate ($code = false) {
if (!empty ($code)) {
// if I printr $user I get the right user
$user = $this->User->find('first', array('activation_key' => $code));
if (!empty($user)) {
$this->User->set(array (
'activation_key' => null,
'active' => 1
));
if ($this->User->save()) {
$this->render('activation_successful');
} else {
// I get this error
$this->set('status', 'Save error message');
$this->set('user_data', $user);
$this->render('activation_fail');
}
debug($this->User->invalidFields());
} else {
$this->set('status', 'Account not found for this key');
$this->render('activation_fail');
}
} else {
$this->set('status', 'Empty key');
$this->render('activation_fail');
}
}
When I try the action test.com/users/activate/hashedkey I get the activation_fail template page with Save error message message.
If I printr the $user var I get the right user from cake's find method.
Where I'm wrong?
I think the problem may be in the way you're querying for the User record. When you do this:
$user = $this->User->find('first', array('activation_key' => $code));
The variable $user is populated with the User record as an array. You check to ensure it's not empty, then proceed; but the problem is that $this->User hasn't been populated. I think if you tried debug($this->User->id) it would be empty. The read() method works the way you're thinking.
You could try using the ID from that $user array to set the Model ID first, like so:
if (!empty($user)) {
$this->User->id = $user['User']['id']; // ensure the Model has the ID to use
$this->User->set(array (
'activation_key' => null,
'active' => 1
));
if ($this->User->save()) {
...
Edit: Well another possible approach is to use the $user array instead of modifying the current model. You said that you get back a valid user if you debug($user), so if that's true you can do something like this:
if (!empty($user)) {
$user['User']['activation_key'] = null;
$user['User']['active'] = 1;
if ($this->User->save($user)) {
...
This method works in the same way as receiving form data from $this->request->data, and is described on the Saving Your Data part of the book.
I'm curious though if there's another part of your setup that's getting in the way. Can other parts of your app write to the database properly? You should also check to make sure you aren't having validation errors, like their example:
<?php
if ($this->Recipe->save($this->request->data)) {
// handle the success.
}
debug($this->Recipe->validationErrors);