GithubHelp home page GithubHelp logo

Comments (12)

NeoBlack avatar NeoBlack commented on June 15, 2024

Hi @jacmgr
the Meta and Page models implement the ArrayAccess interface, so you can use this model classes (objects) as an array.

from phile.

jacmgr avatar jacmgr commented on June 15, 2024

php 5.3.5; Every time I tried I can't get them to work as arrays. If you can post a 2 liner of getting each page meta data into an array it would help me a lot. Using in the template_registered event, the after_read_file_meta, and after_load_content events. I browsed existing phile plugins but no clue for me.

from phile.

NeoBlack avatar NeoBlack commented on June 15, 2024

currently I could not test it, but I think the following code should work:

$pages = \Phile\Repository\Page::findAll();
foreach ($pages as $page) {
    $metaModel = $page->getMeta();
    forach ($metaModel as $metaProperty => $metaValue) {
        echo "{$metaProperty}: {$metaValue}";
    }
} 

from phile.

jacmgr avatar jacmgr commented on June 15, 2024

Sorry way past bedtime here. I used similar code to above and could not get to work. I'll try more tomorrow, but if you get any ideas. THankyou

from phile.

jacmgr avatar jacmgr commented on June 15, 2024

Tried:

if ($eventKey == 'template_engine_registered') {

   $pages2 = \Phile\Repository\Page::findAll();
    foreach ($pages2 as $page) {
        $metaModel = $page->getMeta();
        foreach ($metaModel as $metaProperty => $metaValue) {
            echo "{$metaProperty}: {$metaValue}";
        }  
    }

} 

Got These errors

Strict Standards: Non-static method Phile\Repository\Page::findAll() should not be called statically, assuming $this from incompatible context in C:\xampp\htdocs\phile\philecms\plugins\phileJacWikittenTree\plugin.php on line 48

Fatal error: Call to undefined method PhileJacWikittenTree::getPage() in C:\xampp\htdocs\phile\philecms\lib\Phile\Repository\Page.php on line 72

Then tried:

$pageRespository = new \Phile\Repository\Page();
$pages  =  $pageRespository->findAll();                
foreach ($pages as $page) {
    $metaModel = $page->getMeta();
    foreach ($metaModel as $metaProperty => $metaValue) {
        echo "{$metaProperty}: {$metaValue} <br>";
    }
} 

No errors but no output

Then tried:

$pages  =  $data['data']['pages'];                
foreach ($pages as $page) {
    $metaModel = $page->getMeta();
    foreach ($metaModel as $metaProperty => $metaValue) {
        echo "{$metaProperty}: {$metaValue} <br>";
    }
} 

Got No output.

from phile.

jacmgr avatar jacmgr commented on June 15, 2024

newbie at github, didn't mean to close this!

from phile.

james2doyle avatar james2doyle commented on June 15, 2024

Check out this template parser lib. I converted the pages object to an array for the template vars.

https://github.com/PhileCMS/phileTemplateLex/blob/master/lib/Phile/Template/Lex.php#L46

from phile.

NeoBlack avatar NeoBlack commented on June 15, 2024

yes, my code example was wrong, as the error tells you, the method findAll is not a static method.
As you can see in the example from James, this code should work:

$pageRepository = new \Phile\Repository\Page();
$pages = $pageRepository->findAll()
foreach ($pages as $page) {
    $metaModel = $page->getMeta();
    forach ($metaModel as $metaProperty => $metaValue) {
        echo "{$metaProperty}: {$metaValue}";
    }
} 

from phile.

james2doyle avatar james2doyle commented on June 15, 2024

I would recommend using the objects anyway. The amount of time put into converting to an array, at this point, seems like it would have been just as easy to re-write to use objects. The event system is also quite different, which means you will have to replace that code too.

Another note, is that Phile uses a FastCache (when enabled), which caches the Page::findAll() method. So converting the object to an array, every time, will mean that the cache will never get used. Unless you write in a cache check into the plugin. The reason I had to do it for that template parser, is because that parser didn't support objects.

If you need help converting more plugins we really do want to help. But just hacking a Pico plugin, isn't really the best way to make them work efficiently.

That being said I still need to go back and add the cache service to those template parsers.

I dont want to turn you away from converting all your plugins, I am saying thank you and just giving some advice.

from phile.

jacmgr avatar jacmgr commented on June 15, 2024

No problem. As you see, my post above already tried the newer example you posted. If your intention is that that should work, I believe it does not.

I thought that Phile models implemented the ArrayAccess interface, so you can use this model classes (objects) as an array if you wanted. So if that is not the case, then no problem. But if the intention of the core code was that the above examples should work, well, they did not for me. Does your example snippet actually work for you? If not, then what code should be used to loop through the meta?

Close comment or continue discussion, up to you.

from phile.

james2doyle avatar james2doyle commented on June 15, 2024

I've figured it out. The data that was returned in $page->getMeta() was protected. You needed to access it via the getAll() on the returned class from getMeta(). So here is the working code:

$pageRepository = new \Phile\Repository\Page();
$pages = $pageRepository->findAll();
foreach($pages as $page) {
        // getMeta and the getAll returns an array of the data
    $metaModel = $page->getMeta()->getAll();
        // show me the meta attributes
    var_dump($metaModel);
}

It was actually in the example I linked. On this line. I can honestly say that I wasn't quite sure why it wasn't working. But now I can say I understand Phile a little better haha

from phile.

jacmgr avatar jacmgr commented on June 15, 2024

Excellent. That is what I was looking for. I tested and it works for me. I am going to sleep now! Thankyou.

from phile.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.