How to handle index undefined errors in CakePHP? - php

I got an old PHP system integrated with a new CakePHP one. The problem is when displaying data I occasionally get index undefined errors meaning the models aren't related to where they're supposed to be maybe because there's no validation. There are gaps resulting into models loosing their relationship with a model I'm expecting hence te index undefined error.
What do you guys think is the best way to remedy this issue if let's say I can't touch the old PHP system?

You can use if (isset($post['Comment'])) style checks before using indexes that may or may not exist as well.

First, I think empty() is now preferred to isset() as empty checks for nulls, zeroes, and empty strings, as well as whether or not something is set.
Second, if your issue is relating the models then debug($this->model1->model2) is your best friend. It will show you if the models are properly related (It should state model2 object in bold if all is well, anything else and your relations aren't working.).
If you can't touch the old system, you can still make it work and look like cake by creating a cake model for it. Set the model to use no table, and build functions that will shape the resulting arrays and objects into cakephp arrays. Essentially, create a cake wrapper for the old functions. You can use the inflector class to help automate that a bit

You could potentially use the model's afterFind callback to massage your data into the correct form.

Related

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.

Subsets of properties in a Swagger definition

I'm writing an API where I have a Controller that POSTs a new object, GETs it back and can PUT/PATCH updates to it. The problem is that there's a difference in properties between the two different actions. For example, when I POST a new object I want to ensure that the 'id' of it is returned so that it can be used to identify it for the GET/PUT/PATCH endpoints. It doesn't matter if it comes back via the GET (it's just a duplication of data at that point) but I certainly don't want it passed for the PUT or PATCH as the id is immutable.
So what's the best way to mark this up in swagger so that I can have different versions of the same Definition? I've seen that you can use 'allOf' to add Definitions to other properties, but I'm wondering if there's a way of saying 'not these properties in the definition'?
If I could do the latter I could make one Definition of the object as a whole and simply knock out the things that aren't required to be returned or submitted when referencing it at the Controller. Is this possible? Am I making sense?
(Just to make things more interesting, my swagger.json file is being generated by swagger-php based on Annotations in my controller and entity files)
I am facing the same issue while writing our spec file, and didn't know how to fix it but what I used is the property "readOnly":true, this way the documentation says that this is a readonly property, you can only read it through GET/POST methods but you cannot send it via PATCH/ PUT.. hope this helps

Where To Put Arrays For Common Data Storage In Symfony

I'm brand new to Symfony but am loving getting familiar with it (and many of the concepts behind it). MVC is pretty new to me in terms of the way I'm encountering it in Symfony.
My question is that if I have a simple array of commonly used data that I don't think necessarily belongs in a database table where should I store this. Is it an Entity? Should I store it in the Should I put it in the controller? Somewhere else?
I'm talking specifically about something like a US States array that I might use to power a dropdown. Right now I'm having to build an entity and store these in the database but would like to know if there is a better / preferred way to do this.
In my procedural days I would keep a file called "includes/arrays.php" and pull that when I needed one of these.
Thanks
If you want to use this data with other Entities, for example State would be connected to Adress object, I would stick with Entities, because it makes relations easier to implement and work with (I assume you using some kind of ORM e.g. Doctrine).
If you don't want to use this data with other entities, maybe you would like to hardcode them into all the templates somehow. http://symfony.com/doc/current/cookbook/templating/global_variables.html (I assumed you are using Twig).
A similar question was answered here:
Where to define static array related to an entity in symfony2 ?
It depends. I would opt by having that kind of data in the database. Suppose you in the future would have a back-office that update data.
Or you could use config files. For example, in yml format, arrays is easy to define.
Just like #foxtrot said, any data that is changeable should be stored in the database, just so you do not have to edit any code when a change occurs.
Firstly, I would create the Entity for the common data, and then I would use Fixtures to generate the entries in the database when you deploy your code.
This way, you allow later editing through either forms or phpMyAdmin, but you also get to write the default values into a PHP class so you don't have to manually enter all of them into the database.
See Symfony - DoctrineFixturesBundle

PHP Validating a lot of fields on obj instantiation

I have a class that represents data returned from a RESTful API. The data returned may contain a lot of different fields or arrays that I want to represent with my object. There may be something like 20 different fields I may have to initialize when the object is created. Some of those fields may be empty depending on the ID I'm trying to get back. I need to do some basic validation to make sure I'm only initializing the fields for values that exist. A simple null/empty check should suffice for this but I don't want a lot of repeated code.
Is there any way to easily accomplish this with magic methods or do I need to manually validate everything with a helper method of some sort?
a couple mouth ago I was worked on a Rest JSON API Wrapper.
Array with loop
My first Idea was to put all my field in a array and validate it with a loop to check the data integrity,
but really specific field like only three string value possible, or one integer with 3 possible value, this method is not enough.
Specific container
So I build specific object container with a specific test in all my field in my object constructor.
The code seams to be very heavy but really simple and obvious if you make if condition one by one (not in cascade).
You can avoid mistype issue or copy past bugs with unit test, and cancel the construction of the object if something is missing or wrong.
JSON Validation
I assume you use json or xml to exchange data between your code and the REST API,
seam to be obvious but JSON validation first give a good idea if you get all your information.
Hope it's help
Regards

Trying to persist objects with MongoDB

First things first. I may be completely off track with this. I'm still learning with Mongo and NOSql solutions in general. We have a new component to our app we are writing and instead of sitting down and messing with a relation database we wanted to use something that would fit better with our objects.
So let's take a simple example:
Class User extends \Model {
public $name;
public $hobbies;
}
$name would just be a string. But lets say $hobbies is an object or an array of objects. I want to just be able to throw this into a data store and be able to retrieve it later.
At first I went down the road of breaking the object down into an array and storing that in Mongo and then pulling it back out and populating an object. Pretty simple with a generic import and export method I made. The problem comes when I have some robust objects that have other objects as member variables and so on. At that point I could still export into a multidimensional array and store it fine. But importing back into the objects became problematic.
The other option I could do is just seralize() the object and store that in mongo along with some descriptive data.
Sooooo. Thoughts?
Part of my problem here is that I'm new to the NOSql products and not sure their full limitations/potential. Am I just looking at Mongo wrong and trying to make it do something it's not meant to do? I'd prefer not to use some 3rd party module and would rather write something simple and lightweight.
Although I didn't want to use a 3rd party app, Doctrine's ODM for Mongo seems to do exactly what I wanted. Got it set up and seems to be working good so far.
http://www.doctrine-project.org/projects/mongodb-odm.html
I think serialize is the way to go here. Then you can use the magic methods __sleep and __wakeup for each class to handle any tricky situations.
The other option here to serialize your objects into arrays instead of just using "serialize". If I'm not mistaken, you can actually override the "serialize" method in these sub-objects and basically have them serialize themselves into arrays (or more specifically hash-tables).
If Doctrine does this for you, then all the better. But if you just want this feature you can probably cook your own.

Categories