如何从表中获取数据,该数据在cakePHP的联接表中不可用

时间:2019-01-29 07:45:00

标签: php mysql cakephp

我正在使用cakePhp 3.6,我有两个表。两者都加入了。在一个表中,我有一些token的表名是codes,在另一个表中,我为某个场所存储了一些code。表名称为venues_codes
就像我在code表中有10个codes。并且在venues_codes表5中存储了code以便有所作为。 这是venues_codes表的结构。 id venue_id code_id。 这里的venue_id来自另一个表。现在不用担心了。 我只想从code表中无法获得的codes表中剩下的五个venues_codes

这是我尝试过的方法,但这只是获取两个表中都可用的token

$this->loadModel('VenueCodes');
$this->loadModel('Codes');
$query = $this->VenueCodes->find()
->contain(['Codes'])
->select(['id','code_id']);`

我正在使用CakePHP。但是,如果有人可以用常规的sql逻辑帮助我,那对我也将有所帮助。

2 个答案:

答案 0 :(得分:1)

这应该为您提供所有未连接到场所的代码:

// dummy class used for returning response
public class TaskResult
{
    public int status { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        //destination folder
        var desFolders = new List<string>
        {
            @"\\Files\00a807b3\013413a0\012216ad",
            @"\\Files\00a807b3\013413a0\012216ad\IdoNotExist" // to trigger failure case, IdoNotExist => does not exist
        };

        //base 64 string
        var content =
            "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==";

        var t1 = Task.Run(() => WriteFile(desFolders[0], content));
        var t2 = Task.Run(() => WriteFile(desFolders[1], content));

        // to track if file was ever saved
        bool anySuccess = false;

        int result = Task.WaitAny(t1, t2);

        if (result == 0)
        {
            var response = t1.Result;
            if (response.status == 1)
            {
                anySuccess = true;
                t2.ContinueWith(prevTask =>
                {
                    var t2Res = prevTask.Result;
                    if (t2Res.status == 0) // post processing if t2 could not save file
                        PostProcessing("t2");
                });
            }
        }
        else if (result == 1)
        {
            var response = t2.Result;
            if (response.status == 1)
            {
                anySuccess = true;
                t1.ContinueWith(prevTask =>
                {
                    var t1Res = prevTask.Result;
                    if (t1Res.status == 0)
                        PostProcessing("t1"); // post processing if t1 could not save file
                });
            }

        }


        // to check if completed task was not a success then wait for other running task 
        if (!anySuccess)
        {
            TaskResult waitedResponse = null;
            if (result == 0)
            {
                t2.Wait(); // wait for t2 as t1 was already completed
                waitedResponse = t2.Result;
                if (waitedResponse.status == 1)
                {
                    //if t2 was successful then post process t1
                    anySuccess = true;
                    t1.ContinueWith(prevTask => { PostProcessing("t1"); });
                }
            }
            else
            {
                t1.Wait();
                waitedResponse = t1.Result;
                if (waitedResponse.status == 1)
                {
                    //if t2 was successful then post process t1
                    anySuccess = true;
                    t2.ContinueWith(prevTask =>
                    {
                        var val = prevTask;
                        PostProcessing("t2");
                    });
                }
            }

        }

        if (!anySuccess)
        {
            //return failure;  
        }

        //return success


    }

    private static void PostProcessing(string t)
    {
        Console.WriteLine( $"post processing : {t}");
        Console.ReadLine();
        // insert record in database 
        // intentionally just not to make complex no argument is recived  for insertion
    }

    private static TaskResult WriteFile(string folder, string content)
    {
        var imageName = $"{Guid.NewGuid().ToString()}.jpg";
        var fullName = $@"{folder}\{imageName}";

        try
        {
            byte[] bytes = Convert.FromBase64String(content);
            using (var imageFile = new FileStream(fullName, FileMode.Create))
            {
                imageFile.WriteAsync(bytes, 0, bytes.Length);
                imageFile.FlushAsync();
            }

            return new TaskResult { status = 1 };
        }
        catch (Exception ex)
        {
            return new TaskResult { status = 0 };
        }
    }

}

答案 1 :(得分:-1)

纯mysql查询如下:

 SELECT code FROM codes WHERE code NOT IN (SELECT code_id FROM venues_codes);