Jump to:

325 Posts in 200 Topics by 157 members

Migrating a Site to Silverstripe

SilverStripe Forums » Migrating a Site to Silverstripe » integrate external PHP code

What you need to know when migrating your existing site to SilverStripe.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2
Go to End
Author Topic: 5862 Views
  • UncleCheese
    Avatar
    4085 Posts

    Re: integrate external PHP code Link to this post

    So do this:

    function StaffSearchForm()
    {
    return new Form(
    $this,
    "StaffSearchForm",
    new FieldSet(new TextField('FirstName','First Name'), new TextField('LastName','Last Name')),
    new FieldSet(new FormAction('doStaffSearch','Search'))
    );
    }

    function doStaffSearch($data, $form)
    {
    $results = DataObject::get("SiteTree","ClassName IN('FacultyPage','StudentPage','StaffPage'), lastname LIKE '%".$data['FirstName']."%' AND firstname LIKE '%".$data['LastName']."%'");
    return $this->customise(array('Results' => $results))->renderWith(array('StaffSearch_results','Page'));
    }

    You really should be using MATCH and AGAINST in place of like because it is built for searching. If someone misspells a teacher's name by one letter, your query will return null. the MATCH/AGAINST functions are built to tolerate user ambiguity and order your results by relevance.

    If you're getting a white screen, you need to turn up your error reporting in PHP and find out what's going on. Probably a syntax error somewhere.

  • Victor
    Avatar
    Community Member
    128 Posts

    Re: integrate external PHP code Link to this post

    It looks like error if search returns empty is due to collision with SiteSearchForm of blackcandy. Commenting it out solves the problem. How to avoid this collision?

    Error message:

    Trace

    Page_Controller->results()
    call_user_func_array(Array,Array)
    Line 551 of ViewableData.php
    ViewableData->cachedCall(Results,,)
    Line 1003 of ViewableData.php
    ViewableData_Customised->cachedCall(Results,,)
    Line 592 of ViewableData.php
    ViewableData->hasValue(Results)
    Line 11 of .cache.Applications.MAMP.htdocs.SilverStripe.themes.blackcandy.templates.Layout.FacultySearch_results.ss
    include(/private/var/tmp/silverstripe-cache-Applications-MAMP-htdocs-SilverStripe/.cache.Applications.MAMP.htdocs.SilverStripe.themes.blackcandy.templates.Layout.FacultySearch_results.ss)
    Line 354 of SSViewer.php
    SSViewer->process(ViewableData_Customised)
    Line 346 of SSViewer.php
    SSViewer->process(ViewableData_Customised)
    Line 774 of ViewableData.php
    ViewableData->renderWith(Array)
    Line 28 of FacultyHolder.php
    FacultyHolder_Controller->doFacultySearch(Array,Form,HTTPRequest)
    Line 228 of Form.php
    Form->httpSubmission(HTTPRequest)
    Line 107 of RequestHandler.php
    RequestHandler->handleRequest(HTTPRequest)
    Line 121 of RequestHandler.php
    RequestHandler->handleRequest(HTTPRequest)
    Line 122 of Controller.php
    Controller->handleRequest(HTTPRequest)
    Line 28 of ModelAsController.php
    ModelAsController->handleRequest(HTTPRequest)
    Line 277 of Director.php
    Director::handleRequest(HTTPRequest,Session)
    Line 121 of Director.php
    Director::direct(/faculty/FacultySearchForm)
    Line 115 of main.php
    ****
    Thanks! Victor

  • UncleCheese
    Avatar
    4085 Posts

    Re: integrate external PHP code Link to this post

    Try changing the name of the template variable so it doesn't collide.

    function doStaffSearch($data, $form)
    {
    $results = DataObject::get("SiteTree","ClassName IN('FacultyPage','StudentPage','StaffPage'), lastname LIKE '%".$data['FirstName']."%' AND firstname LIKE '%".$data['LastName']."%'");
    return $this->customise(array('StaffSearchResults' => $results))->renderWith(array('StaffSearch_results','Page'));
    }

    <% if StaffSearchResults %>
    <% control StaffSearchResults %>

    etc...

  • Victor
    Avatar
    Community Member
    128 Posts

    Re: integrate external PHP code Link to this post

    Thanks! It fixes the original form. Had no chance to work with more advanced form

    Victor

  • Victor
    Avatar
    Community Member
    128 Posts

    Re: integrate external PHP code Link to this post

    Thanks again. I am making progress but not completely.

    1) I tried MATCH AGAINST. SilverStripe returned an error but MySQL query via phpMyAdmin returned

    "Can't find FULLTEXT index matching the column list".

    So, this closes the opportunity unless there is a way to force making such index

    2) Currently I use only StaffPage and this works:

    function doStaffSearch($data, $form)
    {
    $results = DataObject::get("StaffPage","lastname LIKE '%".$data['LastName']."%' AND firstname LIKE '%".$data['FirstName']."%'");
    return $this->customise(array('StaffSearchResults' => $results))->renderWith(array('StaffSearch_results','Page'));
    }


    but this does not:

    function doStaffSearch($data, $form)
    {
    $results = DataObject::get("SiteTree","ClassName IN('StaffPage'), lastname LIKE '%".$data['LastName']."%' AND firstname LIKE '%".$data['FirstName']."%'");
    return $this->customise(array('StaffSearchResults' => $results))->renderWith(array('StaffSearch_results','Page'));
    }

    I am away from my office today and despite putting other IPs and hostnames into developer sites in _config.php I can get verbose output only on localhost. Tomorrow I (hopefully) will get it

    Victor

  • Victor
    Avatar
    Community Member
    128 Posts

    Re: integrate external PHP code Link to this post

    OK I found an error: it should be AND instead of ,

    function doStaffSearch($data, $form)
    {
    $results = DataObject::get("SiteTree","ClassName IN('FacultyPage','StudentPage','StaffPage') AND lastname LIKE '%".$data['FirstName']."%' AND firstname LIKE '%".$data['LastName']."%'");
    return $this->customise(array('StaffSearchResults' => $results))->renderWith(array('StaffSearch_results','Page'));
    }

  • Victor
    Avatar
    Community Member
    128 Posts

    Re: integrate external PHP code Link to this post

    And when I thought everything is fine ... I am confused:

    StudentPage.php and FacultyPage.php are almost identical with the difference that they declare

    < class StudentPage extends Page {

    > class FacultyPage extends Page {

    < class StudentPage_Controller extends Page_Controller {
    ---
    > class FacultyPage_Controller extends Page_Controller {

    Then StudentHolder.php

    class StudentHolder extends Page {
    static $db = array(
    );
    static $has_one = array(
    );

    static $allowed_children = array('StudentPage');
    }

    class StudentHolder_Controller extends Page_Controller {

    function StudentSearchForm()
    {
    return new Form(
    $this,
    "StudentSearchForm",
    new FieldSet(new TextField('FirstName','First Name starts from'), new TextField('LastName','Last Name starts from')),
    new FieldSet(new FormAction('doStudentSearch','Search'))
    );
    }

    function doStudentSearch($data, $form)
    {
    $results = DataObject::get("SiteTree","ClassName IN('StudentPage') AND lastname LIKE '".$data['LastName']."%' AND firstname LIKE '".$data['FirstName']."%'");
    return $this->customise(array('StudentSearchResults' => $results))->renderWith(array('StudentSearch_results','Page'));
    }
    }

    ?>

    and FacultyHolder.php is a copy with Student replaced by Faculty everywhere.

    Then

    StudentSearch_results.ss

    <div class="typography">
    $StudentSearchForm
    <% if StudentSearchResults %>
    <ul>
    <% control StudentSearchResults %>
    <li><a href="$Link">$firstname $lastname</a></li>
    <% end_control %>
    </ul>
    <% else %>
    There were no results that matched your query.
    <% end_if %>
    </div>

    and FacultySearch_results.ss is again just replacement Student to Faculty

    Here is a strange part:

    if I put either StudentPage.php, StudentHolder.php or FacultyPage.php, FacultyHolder.php into code and flush db, the corresponding search works perfectly.

    if I put StudentPage.php, StudentHolder.php and FacultyPage.php, FacultyHolder.php into code and flush db both searches end up with SilverStripe error

    The tables in db should be completely independent but the fields have the same names. May be this is the source of error?
    However we have a lot of resons to have the same field names in both tables

    Any suggestions? Thanks you in advance. Victor

    5862 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.