symfony sfDoctrineRoute model question - php

I couldn't understand completely how does the sfDoctrineRoute class works
for example, i have the following route:
Comment:
class: sfDoctrineRouteCollection
options:
prefix_path: :username/comment
module: comment
model: Comment
now, in executeNew() method of commentActions class, this code:
$this->getRoute()->getObject()
will return the first Comment object in my database. of course i can manually create a new Comment() object, but then what's the benefit of using the sfDoctrineRoute class instead of sfRoute?

In the case of executeNew, there is little/no benefit to using a doctrine route.
Consider instead the executeEdit method (update, delete and show are the same too).
A url could be like:
/comment/5/edit
(or in your case, /myusername/comment/5/edit)
$this->getRoute()->getObject() will then return comment 5 from the database - saving you the trouble of loading it (only a line or 2 of code, but still). And, a neat feature, if there is no comment 5 in the database, it automatically handles this and causes a 404 error - so you don't need to worry about that either.

Related

How to override a model class in PyroCMS (Laravel, PHP)?

I installed PyroCMS and am extending it to make it into a Learning Management System (LMS) where only logged-in users can view the pages, and the pages also only begin to be viewable a variable number of days after a user enrolls in the course.
(I.e., Module 1's Lesson 1 may unlock and be visible immediately, but Lesson 2 could be configured to be hidden until 1 day later, and Lesson 3 might become visible X days later, etc.)
How I achieved this was by writing a Laravel package with this migration:
Schema::table('pages_pages', function (Blueprint $table) {
$table->string('drip_delay')->nullable()->after('str_id');
});
I then created a DrippablePagesServiceProvider class with this in the boot() function:
$this->app->bind('Anomaly\PagesModule\Http\Controller\PagesController', 'me\DrippablePages\PagesController'); //https://laravel.com/docs/5.6/container#binding
I designed my custom PagesController to show a special view whenever the logged-in user is trying to access a page too early. This functionality is all working totally fine.
But instead of editing the drip_delay field directly in the database like I've been doing, I'd prefer to be able to edit right alongside the other fields at the /admin/pages/edit/4 URL.
I'm pretty sure I need to override various parts of PagesModule, such as PageEntryFormSections (doc). And I think I have that working.
But when stepping through with Xdebug, I see that the PageModel that gets looked up at this line (via dependency injection?†) in edit() within Http\Controller\Admin\PagesController still doesn't show my new drip_delay field.
How can I override PageModel or do whatever I need to do so that it shows the drip_delay field in this Admin panel view?
† Laravel docs about container and controllers imply this.
To override a model first you need a new one which extends a model you want to override:
<?php namespace Ryan\ExtenderModule\Post;
class PostModel extends \Anomaly\PostsModule\Post\PostModel
{
}
Then inside the ServiceProvider you need to bind it reversed:
<?php namespace Ryan\ExtenderModule;
use Anomaly\PostsModule\Post\PostModel;
use Anomaly\Streams\Platform\Addon\AddonServiceProvider;
class ExtenderModuleServiceProvider extends AddonServiceProvider
{
protected $bindings = [
PostModel::class => \Ryan\ExtenderModule\Post\PostModel::class,
];
}
That's all. Good luck ))

FOS REST Bundle: funny thing with a "persons" resource

I'm using FOS Rest bundle to create a REST resource for a "persons" resource, basically the urls are meant to be:
List: GET /api/persons
Add: POST /api/persons
Get single person: GET /api/persons/{id}
Modify: PUT /api/persons/{id}
Delete: DELETE /api/persons/{id}
So I defined my methods in the controllers as follows:
public function cgetPersonsAction() # List
public function cgetPersonAction(...) # Get single
public function cdeletePersonAction(...) # Delete
#etc...
And here comes the funny part, instead of /api/persons for get single, put, post and delete FOS Rest bundle calculates the plural of person into people instead of persons and the urls ended up being:
List: GET /api/persons
Add: POST /api/people
Get single person: GET /api/people/{id}
Modify: PUT /api/people/{id}
Delete: DELETE /api/people/{id}
I searched the code looking for maybe some people/person in the bundle but I found nothing, so I guess it must be related with some php plural function.
Do you know if there's any way to force the url to remain being "person"? I think people doesn't make too much sense here
You can force the url by using:
FOS\RestBundle\Controller\Annotations\Get; ...\Post; ,...
For GET url it would be:
#Get("api/whatever/{id}")

Controller check requirements to process action

I'm trying to figure out if there is possibility to check requirements before processing controller action. In Nette there are methods like checkRequirements, onStartup, beforeRender where I can check this.
I have api resource album/{albumId}/song/ and I would like to check if album with given id exists every time any action on my SongController is processed and return 404 status code if not.
So far I have found this article in Symfony documentation where I found there are no methods like preExecute and postExecute. However I guess there is bundle or something like that to add those methods. I think it does not make sense to create new class to use it only in one controller.
Are there any other options to do it?
ParamConverter does that. It looks for a entity using the id supplied from the route and throws an exception, returning a 404 if it doesn't find anything.
// paramConverter requires that you type-hint a class, which is a best practice anyway :)
public function getArtist(\Appbundle\Entity\Song $song)
{
//...
}

Phalcon pdo excpetion model first

I have problem with phalcon framework namely with models methods...
As you know models has included methods find() and findFirst()
I have generated model with phalcon-dev tools and now I am trying to do Model::find on it but I am getting an exception but dont know why...
There is some more informations (e.g stacktrace) :
http://exception.mateuszmarzecki.pl/
You can try change methods in model file
public static function find($parameters = array())
{
return self::find($parameters);
}
Does not look like your passing it the right parms.
SELECT FROM `nacionality`
Notice that your not selecting any fields from the database, and that is why your getting the Exception.
So... after some time of debugging I've found the problem...
For the next generation... if you don't want to lose a week as I did. Just read carefully your application config.
Problems occurs because I missed table and column annotations as well.
In my application config I have something like:
$metaData->setStrategy(new \Engine\Db\Model\Annotations\Metadata());
so Phalcon was looking for annotations in my model files, more info about this you can find there:
https://forum.phalconphp.com/discussion/1933/column-types-for-model-annotations
Happy New Year

pagination and factory in laravel4

Consider this code taken from here.
public function getIndex()
{
$posts = Post::orderBy('id','desc')->paginate(10);
// For Laravel 4.2 use getFactory() instead of getEnvironment() method.
$posts->getEnvironment()->setViewName('pagination::simple');
$this->layout->title = 'Home Page | Laravel 4 Blog';
$this->layout->main = View::make('home')->nest('content','index',compact('posts'));
}
As I understand it, pagination limits the number of rows, so I think paginate(10) means select first ten rows in the database. But I absolutely don't understand this.
// For Laravel 4.2 use getFactory() instead of getEnvironment() method.
$posts->getEnvironment()->setViewName('pagination::simple');
or
$posts->getFactory()->setViewName('pagination::simple');
And everything below. Mainly I don't understand what factory means and how it relates to pagination. I went to the laravel docs on Illuminate\Pagination\Factory and Illuminate\View\View but I can't find the meaning of factory. Can anyone explain the code above?
You are essentially setting how the pagination is output in HTML by selecting a specific paginator view, this allows you to have more than one type in an application or use different to the default.
Using multiple pagination types in the same application
Sometimes, you may want to use different pagination types across your
application. By default, Laravel will use the type specified in your
app/config/view.php file, so you need to override this setting when
you wish to use another type. Here is how to do so.
// This code should be in a controller or a route Closure.
// Let’s use the good old example of a list of blog posts.
$articles = Article::paginate(5);
Paginator::setViewName('pagination::simple');
/*
Alternatively, you could also use this to achieve the same result:
$articles->getEnvironment()->setViewName('pagination::simple');
For those who would like to know what’s happening under the hood, here is a more
detailed explanation:
1. Calling paginate() on an Eloquent model or a query builder will return an
instance of \Illuminate\Pagination\Paginator
2. Then, we need to get the related \Illuminate\Pagination\Environment of this
paginator via the well-named getEnvironment() method.
3. Finally, we can specify the pagination type we need. The default value is
'pagination::slider'.
The pagination types that are available by default are located in the
vendor/laravel/framework/src/Illuminate/Pagination/views directory.
*/
Source: http://laravel-tricks.com/tricks/using-multiple-pagination-types-in-the-same-application

Categories