PHP, Redis and ORM? - php

I know that ORM and redis is weird enough but. My redis server is a storage (not temporary). So I discovered some queries and commands but now I want to know how to use it in best way. As everybody know ORM is good enough (ActiveRecord, Doctrine2, etc.). So give me your thoughts how to create model and use it with redis db.
My thoughts is to create some abstract class that will load (predis) and work with it BUT I have no idea how to make checker\manager of 'columns' (yes I know that redis has not such things as columns), simple example, we call this one
hset('user:id_111', 'username'. 'admin')
hset('user:id_111', 'password'. 'pass')
hset('user:id_111', 'email'. 'some#mail.com')
hset('user:id_111', 'confirmed'. '1')
After that we should have model that will return all method we need (they will be writted manually)
$oModel->getUserName();
$oModel->getPassword();
$oModel->getEmail();
$oModel->getConfirmed();
So now question is of calling queries, basically in abstract model we should create Predis\Client() but hey, it will be created in EACH model (is it bad?)!
Also do we have check ALL vars\values when changing data in model, simple example what should we do on incorrect data? Exception or simply not saving model and save error in model?
Any ideas\thoughts you have, please share.
PS: no need code, enough will be description how you see this one

I recently needed to tackle the same problem; I started building an orm for redis. It's still early in development, but might help you out https://github.com/tystr/redis-orm.

Related

Laravel - difference between Controller & Model

I'm learning Laravel and I'm watching many tutorials, but I dont really get it, what's the difference between the controller and model, because you can put in both a function.
Controllers in Laravel are used to determine how to handle http requests.
When you have anything to do with the DB, its better to place those function in the model, and call them from the controller.
In clear terms:
Model performs all operations on data from DB.
Controller call necessary model methods and ready the data.
View take care of displaying the data.
I hope this is clear enough.
You will be familiar with all of this soon.
model methods is for relationships mainly , or to make some thing for every object of this model (database table) every column in db is an object and every table is a model.
but in controller you set your app functionality that you want , and its an intermediator between model and view .
i hop this makes you good in this point.
good luck
You can write functions anywhere, you are perfectly right.
But is not an efficient way to do things.
The answers for those questions can be easily find out. Search about MVC pattern. In few words, remember brief:
MODEL => working with relational databases / storing the data
CONTROLLER => working with the logic(taking inputs, calculus etc) / general functionalities
Combining them is more efficient than working with those together, that is the reason why using a pattern is more great than writing code in a old style mode reinventing the wheel again.

Laravel + MySQL - Storing Eloquent namespaces

I was given this project to work on with absolutely no documentation or contact developer. I noticed in the database dump that they are storing what looks like PHP Namespaces for Eloquent models in a couple tables. For example an address table has a string column named "object_type" with the value always being "App\Entities\Client". I searched through the whole project for the PHP code that would use this value. Hopefully to give me insight to it's purpose. Not to my surprise, the project never uses this value. I just see it hard-coding these values upon insert into the DB.
My question is, is this some sort of Database and/or ORM modeling design practice? If so, could you explain how this could be used in a simple practical sense?
Maybe this was some concept the developer had and it never evolved. It's interesting idea but, the idea of joining through MySQL on a string conditional sounds like torture.
Sounds like Laravel polymorphic relationships:
Custom Polymorphic Types.
By default, Laravel will use the fully qualified class name to store the type of the related model.
And, yes, this is a valid modeling technique, though purists rightly argue this technique abuses normal form.
I am not sure what the developers where thinking.
But imagining we are in a forum with thread and replies to each thread. We maybe want to have a Favourites table where we can save replies and threads.
A way to do it would be to have a column in the favourites table called "object_type" (just to use the same term you have in your case) and then when we save an object into the database with eloquent we can use:
$favourite->object_type = get_class($thread); //or get_class($reply) in case we want a reply
$favourite->save();
This way will save the namespace of that class into the database. But laravel will recognise it when we get it from the database.
Hope this cold be helpful.

MySQL one-to-many relationship

For a forum-like website that I'm building, I have PHP classes 'user' and 'post'. A user has an array of posts. How to resemble that connection in a relational database? With a foreign key on 'post', pointing to a 'user'? This makes sense to me because of a one-to-one relationship, whereas 'user' to 'post' would be one-to-many, and so far as I can see. Any thoughts are welcome - thanks!
Your solution is good. I can't see any better option.
Just for clarify;
Now, with OOP, the goal is to work with objects.
So first step is to create object from database. If you want to go further, you can use an ORM and create your DAO. With this layer, you can manipulate object, modify them and finally save them into database with OOP approach. An exemple, Doctrine2 : Doctrine website
Now, for simple projet you do not absolutely need ORM. Just take care to work with object, and, for exemple, create little method in your object: save(...) who Insert in DB your object.
Complexify it by adding update, delete,... and finally go on CRUD system.
I think it's good idea before go directly on ORM, and see you to learn and concept good OO project.
Hope it's will help you (and sorry for my very bad english :) )

How to handle database access in Zend Framework?

To begin: i'm not an expert in Zend Framework and doing something terrible wrong. I'm sure of that. I think there's something wrong with my design patterns.
As an example:
I'm building access management with Zend_ACL (Access Control List)
There are three tables in the database:
roles
resources
permissions
the permissions table handles the role-resources relation.
I made a model for each table, it extends Zend_Db_Table_Abstract. So far so good.
Now in the ACL I load the resources, role and permissions on a page request and add it to the ACL.
Now the part I'm doing something wrong: The way I do it is call methods from the tablemodels that give me the required data. But when I look at my profiler it takes 117 select queries and takes 0.7 seconds just to load the ACL. No queries for the underlying system yet. This can't be good and I'm sure there is a better way. I just can't find anything about this on google or anywhere.
Is there someone who can tell me if I'm doing something wrong and if I am, what I should to to speed it up? Should I load everything in one query to models and let them handle it? How do I do that, are there any examples?
Thanks in advance!
It's not clear exactly what you're doing, but you shouldn't be grabbing generating so many queries.
Some things to think about:
Do you need to load the full ACL on every request? Perhaps you could structure your data-access layer in such a way that it checks for relevant permissions when a user attempts to perform some action.
If you do need the full ACL, you ought to be able to construct a single query to grab all the relevant data. Perhaps eschewing the Zend_Table stuff here and just executing a raw custom query? Not exactly a best-practice, but if you're loading everything, it's just one query.
More concrete code in your question would definitely produce more concrete answers.
If you do need to load all the stuff, why not load it just once and serialize your acl object and keep it in session or cache ?
Every time you need to access your ACL object, check if the cache or the session variable is set or not. If it is set, you can just avoid running the queries and just unserialize the object from session or cache.
I agree this is a dirty solution, but it works and will not run the 117 queries each time.

Anyone else using DB-Schema-Validation for MVC projects?

Ok, inspired in part by CakePHP's Model Validation I created a project which uses a separate database schema file. I didn't like that in CakePHP the model file and the db-schema are combined in the same php file. I like having them separate.
All of my sql updates, inserts, and deletes are first passed by this schema. I wrote update() insert() delete() functions to do this validation automatically.
Why? Because I can accept a post from a visitor and send the un-checked data to my database without having to check it. My schema filters for innocent and not so innocent violations.
Here is an example of a database schema:
<?php
$schemas = array('database_name_hidden'=>array(
'assigned_weeks'=>array(
'id'=>array('id'),
'user_id'=>array('foreign_id','users'),
'week_number'=>array('posint'),
'year'=>array('posint'),
'unit_id'=>array('foreign_id','units'),
'claim_listing'=>array('posint'),
'created'=>array('created'),
'modified'=>array('modified'),
'resort_id'=>array('foreign_id','resorts','required'),
),
'trade_listings'=>array(
'id'=>array('id'),
'assigned_week_id'=>array('foreign_id', 'assigned_weeks','required'),
'listing_assigned_week_id'=>array('foreign_id', 'assigned_weeks'),
'opposite_id'=>array('numeric'),
'listed'=>array('bool'),
'prev_id'=>array('foreign_id','trade_listings'),
'next_id'=>array('foreign_id','trade_listings'),
'listing_email'=>array('email'),
'listing_description'=>array('text'),
'trade_confirmation_number'=>array('text'),
'external_resort_id'=>array('foreign_id','resorts'),
'external_unit_number'=>array('text','size'=>array(1,6)),
'external_start_time'=>array('time_future',),
'external_end_time'=>array('time_future'),
'admin_comment'=>array('text'),
'comment'=>array('text'),
'created'=>array('created'),
'creator'=>array('creator'),
'modified'=>array('modified'),
'modifier'=>array('modifier'),
'resort_id'=>array('foreign_id','resorts','required'),
),
);
?>
Anyone else doing something like this?
I am not sure I really understand the question, but speaking about DB, Validation, Schema, and PHP, I really like the ORM Framework called Doctrine.
(It's the default ORM stack of the PHP Framework symfony, btw ; but can be integrated quite easily with other frameworks -- I've already used it with Zend Framework, for instance)
It's website seems to be down right now (they've been experimenting some high-load-related troubles, those last weeks), but it provides classes/method to do validation of data, based on the YAML-files describing the schema, before inserting data into DB.
You might want to take a look at it...

Categories