ChrisF79
234
26
General

My model is pulling the same data three times instead of looping properly.

Posted 8 months ago by ChrisF79

First, I'm a beginner and an idiot so please keep that in mind. However, I have been stuck on this all day now (trying to learn) but I need help. I know this is long and I'm really sorry. Now that the stage is set...

I have a console command that connects to 3 different servers and pulls records from each. The problem is that my code is pulling the exact same data 3 times.

Here is my command:

<?php
namespace App\Console\Commands;

use App\Models\GetListing;
use App\Models\RetsFeed;
use App\Models\RetsFeedApi;
use Illuminate\Console\Command;

class DownloadRetsData extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'chores:DownloadRetsData';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Downloads the RETS Data';

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        // Download each RetsFeed's Login Info
        $RetsFeeds = RetsFeed::where('active', '=', '1')->get();

        foreach ($RetsFeeds as $RetsFeed)
        {
            $getlistings = new GetListing($RetsFeed);
            $getlistings->downloadListings();
        }
    }

}

When the foreach runs, the $getlistings object does have all 3 server's info inside. So then, the downloadListings() method is:

<?php

namespace App\Models;

use App\Models\RetsFeedApi;
use Illuminate\Database\Eloquent\Model;

class GetListing
{
    public function __construct(RetsFeed $feed)
    {
        $this->feed = $feed;
    }


    public function downloadListings()
    {

        /* Initialize Rets API Object */
        $retsApi = new RetsFeedApi($this->feed);

        /* Run RETS Query for latest listings */
        $rets = $retsApi->findLatest();


        echo $this->feed->board . ": " . $rets->count() . "\n";
    }
}

I threw that echo command in there just for testing and it outputs:

Naples: 1836
Bonita: 1836
FtMyers: 1836

The RetsFeedApi class that is being used is:

<?php

namespace App\Models;

use App\Models\ProcessRetsListings;
use Carbon\Carbon;

class RetsFeedApi {

    public function __construct(RetsFeed $feed)
    {
        $this->feed = $feed;
    }

    public function connectToRetsServer()
    {
        $config = new \PHRETS\Configuration;
        $config->setLoginUrl($this->feed->feedurl);
        $config->setUsername($this->feed->username);
        $config->setPassword($this->feed->password);        

        $rets = new \PHRETS\Session($config);

        $connection = $rets->Login();
        return $rets;
    }



    public function findLatest()
    {
        // connect to server
        $rets = $this->connectToRetsServer();

        // define variables
        $last_query = $this->feed->last_query_at->format("Y-m-d\TH:i:s");
        $tablename = $this->feed->board;


        //query server for all listings after the last_query_at in the database for this feed
        $results = $rets->Search(
            "Property",
                "RES",
                "(
                    (MatrixModifiedDT={$last_query}+)
                )",
            array(
                'QueryType' => 'DMQL2',
                'Count' => 1, // count and records
                'Format' => 'COMPACT-DECODED',
                'Limit' => 1,
                'StandardNames' => 0, // give system names
            )
        );

        return new RetsFeedApiResult($rets, $results);

    }

}


class RetsFeedApiResult {
    public function __construct($rets, $results)
    {
        $this->rets = $rets;
        $this->results = $results;
    }

    public function count()
    {
        return $this->results->getTotalResultsCount();
    }
}

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.