spiral / app

Spiral Framework Skeleton HTTP Application: Queue, Console, Cycle ORM
https://spiral.dev/
MIT License
191 stars 20 forks source link

Getting error when following documentation #14

Closed sujit-baniya closed 4 years ago

sujit-baniya commented 4 years ago

I tried to follow https://spiral.dev/docs/basics-quick-start I changed the PostCommand as mentioned in documentation to this:

namespace App\Command\Seed;

use App\Repository\UserRepository;
use App\Service\PostService;
use Faker\Generator;
use Spiral\Console\Command;

class PostCommand extends Command
{
    protected const NAME = 'seed:post';

    /** @var UserRepository */
    private $users;

    /** @var PostService */
    private $postService;

    /**
     * @param UserRepository $users2
     * @param PostService    $postService
     * @param string|null    $name
     */
    public function __construct(UserRepository $users2, PostService $postService, ?string $name = null)
    {
        parent::__construct($name);
        $this->postService = $postService;
        $this->users = $users2;
    }

    protected function perform(Generator $faker): void
    {
        $users = $this->users->findAll();

        for ($i = 0; $i < 1000; $i++) {
            $user = $users[array_rand($users)];

            $post = $this->postService->createPost(
                $user,
                $faker->sentence(12),
                $faker->text(900)
            );

            $this->sprintf("New post: <info>%s</info>\n", $post->title);
        }
    }
}

And I'm getting following error.

 [Cycle\ORM\Exception\ORMException]                                      
 Unable to find Entity role for repository App\Repository\UserRepository 

What am I doing wrong.

wolfy-j commented 4 years ago

Can you run ‘php app.php cycle’ to make sure that ORM schema generated?

sujit-baniya commented 4 years ago

I'm getting same error when trying to run above command

 [Cycle\ORM\Exception\ORMException]                                      
 Unable to find Entity role for repository App\Repository\UserRepository 
in /home/sujit/Sites/labs/php/go-php/vendor/spiral/framework/src/Cycle/RepositoryInjector.php:56
wolfy-j commented 4 years ago

Can you show me the content of Post entity? Did you set the repository on it?

wolfy-j commented 4 years ago

Check the demo source code here: https://github.com/spiral/demo

sujit-baniya commented 4 years ago

Post.php

<?php
/**
 * {project-name}
 * 
 * @author {author-name}
 */
declare(strict_types=1);

namespace App\Database;

use Cycle\Annotated\Annotation as Cycle;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @Cycle\Entity(repository = "App\Repository\PostRepository")
 */
class Post
{
    /**
     * @Cycle\Column(type = "primary")
     */
    public $id;

    /**
     * @Cycle\Column(type = "string")
     */
    public $title;

    /**
     * @Cycle\Column(type = "text")
     */
    public $content;

    /**
     * @Cycle\Relation\BelongsTo(target = "User", nullable = false)
     * @var User
     */
    public $author;

    /**
     * @Cycle\Relation\HasMany(target = "Comment")
     * @var ArrayCollection|Comment
     */
    public $comments;

    public function __construct()
    {
        $this->comments = new ArrayCollection();
    }
}

Also I noticed that $this->users is not detected: https://i.imgur.com/zxzyEWm.png

Also I noted that UserRepository is not used: https://i.imgur.com/ct8FmiI.png

wolfy-j commented 4 years ago

Can you make sure that php app.php configure executed?

See the list of installation commands at the bottom of this page - https://spiral.dev/docs/basics-quick-start#source-code

If everything is correct you should find runting/cache/cycle.php file with the ORM schema. I just tested and the newly downloaded code works well.

sujit-baniya commented 4 years ago

I cloned the demo project. On demo project I can't run the server.

It's stuck here https://i.imgur.com/pzu8xn6.png

wolfy-j commented 4 years ago

Per your screenshot, you are running the server. Try to open any page http://localhost:8080/posts?paginate[page]=2 (it will fail if you haven't run the migrations)

sujit-baniya commented 4 years ago

The server is not running at all: https://i.imgur.com/LNH7n1J.png

wolfy-j commented 4 years ago

Hmm, is it possible that :8080 forbidden on your machine? Can you change the port in .rr.yaml and try again? Do you run any other apps which use :8080 port?

sujit-baniya commented 4 years ago

other apps were not using 8080 port... I restarted by PC and it worked... So You used constructor to assign UserRepository...

But why the example from documentation didn't work? https://i.imgur.com/NUzVPsZ.png

wolfy-j commented 4 years ago

The example did not work since prototype trait did not know about users property (repository) until the app.php configure executed.

sujit-baniya commented 4 years ago

I copied the Command files from your demo to my sample project. But still I'm getting the same error on my project.. I removed perform on PostCommand and php app.php configure and it works.

But if I replace it to use the content same as yours, it's still same issue

Can you please do me a favor to look over the demo project: https://github.com/itsursujit/go-php

I'm really unable to figure it out... I need to know why is it happening and find the possible solution.

This framework is promising but I need to debug it on why it's not working and possible solution for it before moving ahead.

Thanks

wolfy-j commented 4 years ago

Weird, the code you provided works well for me. http://prntscr.com/rtwlsz

All the commands work and data seeded.

sujit-baniya commented 4 years ago

hmm... let me remove the project and re-clone it.

FYI, I'm using Ubuntu 18.04 LTS

wolfy-j commented 4 years ago

Just tested it on Ubuntu and it works great.

sujit-baniya commented 4 years ago

Okay! After removal of project and reinstalling it. It worked... Don't know why it was not working before and now it's working.

I'll try to replicate the issue

wolfy-j commented 4 years ago

Please let me know if you'll get the right order of actions to cause the issue.

Recommendation: if something weirdly does not work - remove runtime/cache directory.